Google PageSpeed Insights API not working [PHP]

前端 未结 2 360
悲哀的现实
悲哀的现实 2021-01-28 20:17

I\'m a beginner in PHP, so maybe someone could help to fix this ? My web application is showing Google PageInsights API error..

Here\'s the code, I tried to change vers

相关标签:
2条回答
  • 2021-01-28 20:48
    <?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>'; 
    ?>
    
    0 讨论(0)
  • 2021-01-28 20:49

    The code shared by Siren Brown is absolutely correct, except that while getting the scores we need to send the query parameter &strategy=mobile or &strategy=desktop to get the respective results from 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';  
    
    0 讨论(0)
提交回复
热议问题