1

我正在使用谷歌 pagespeed api。但有时它只是在我调用时返回 NULL。我正在使用 PHP 和 CURL 来调用 api。下面是我的代码

$url_req = 'https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=' . $url . '&key=' . $myKEY . '&screenshot=true&snapshots=true&strategy=mobile';
$results = checkPageSpeed($url_req);

以下是 checkPageSpeed 功能代码

function checkPageSpeed($url) {
if (function_exists('file_get_contents')) {
    $result = @file_get_contents($url);
}
if ($result == '') {
    $ch = curl_init();
    $timeout = 0;
    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;
}

谁能帮助我。

PS它在大多数情况下都可以正常工作,但是对于某些从www重定向到非www或反之亦然的URL,它会返回NULL

4

0 回答 0