Google PageSpeed Insights API无效[PHP]

时间:2015-01-21 21:11:44

标签: php api curl google-api pagespeed

我是PHP的初学者,所以也许有人可以帮忙解决这个问题? 我的网络应用程序显示Google PageInsights API错误..

这里是代码,我试图将版本更改为/ 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 个答案:

答案 0 :(得分: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 :(得分:1)

Siren Brown共享的代码绝对正确,除此之外 获取分数时,我们需要发送查询参数&amp; strategy = mobile &amp; strategy = desktop 以从Page speed 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';