2015-01-21 2 views
0

Я новичок в PHP, так что, может быть, кто-то может помочь исправить это? Мой веб-приложение показывает Google PageInsights ошибка API ..Google PageSpeed ​​Insights API не работает [PHP]

Вот код, я пытался изменить версию/v2 /, но он все еще не работал ..

public function getPageSpeed($domain, $api = "") 
     { 
      try 
      { 
       $callback_url = "https://www.googleapis.com/pagespeedonline/v1/runPagespeed?"; 
       $data = array(
        'url'  => 'http://' . $domain, 
        'key'  => (empty($api) ? $_SESSION['GOOGLEAPI_SERVERKEY'] : $api), 
        'fields' => 'score,pageStats(htmlResponseBytes,textResponseBytes,cssResponseBytes,imageResponseBytes,javascriptResponseBytes,flashResponseBytes,otherResponseBytes)' 
       ); 

       $curl_response = $this->curl->get($callback_url . http_build_query($data, '', '&')); 

       if ($curl_response->headers['Status-Code'] == "200") { 

        $content = json_decode($curl_response, true); 
        $response = array(
         'status' => 'success', 
         'data' => array(
          'pagespeed_score' => (int)$content['score'], 
          'pagespeed_stats' => $content['pageStats'] 
         ) 
        ); 

       } else { 

        $response = array(
         'status' => 'error', 
         'msg' => 'Google API Error. HTTP Code: ' . $curl_response->headers['Status-Code'] 
        ); 

       } 
      } 
      catch (Exception $e) 
      { 
       $response = array(
        'status' => 'error', 
        'msg' => $e->getMessage() 
       ); 
      } 
      return $response; 
     } 

ответ

2
<?php 

    function checkPageSpeed($url){  
    if (function_exists('file_get_contents')) {  
    $result = @file_get_contents($url); 
    }  
    if ($result == '') {  
    $ch = curl_init();  
    $timeout = 60;  
    curl_setopt($ch, CURLOPT_URL, $url);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
    $result = curl_exec($ch);  
    curl_close($ch);  
}  

return $result;  
} 

$myKEY = "your_key"; 
$url = "http://kingsquote.com"; 
$url_req = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url='.$url.'&screenshot=true&key='.$myKEY; 
$results = checkPageSpeed($url_req); 
echo '<pre>'; 
print_r(json_decode($results,true)); 
echo '</pre>'; 
?> 
1

В код разделяют Siren Браун совершенно правильно, за исключением того, что при получении баллов нам нужно отправить параметр запроса & стратегию = мобильный или & стратегии = рабочий стол получить повторно Спектральные результаты от скорости страницы API

$url_mobile = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url='.$url.'&screenshot=true&key='.$myKEY.'&strategy=mobile'; 
    $url_desktop = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url='.$url.'&screenshot=true&key='.$myKEY.'&strategy=desktop'; 
Смежные вопросы