我一直在尝试使用php-curl在PHP中使用Google Pagespeed Insights API,并将请求发送到我在本地服务器上创建的.html
网页上。假设我的IP地址是123.456.78.90
;所以这是我的脚本:
$url = "http://123.456.78.90/webproject/test/some.html";
//$url = "https://developers.google.com";
$curl_init = curl_init("https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=$url&screenshot=true&key=***********");
curl_setopt($curl_init, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_init, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
$response = curl_exec($curl_init);
curl_close($curl_init);
$googlepsdata = json_decode($response, true);
echo "response: "; print_r($googlepsdata); echo "<br><br><br>";//check
以下是回复:
response: Array ( [error] => Array ( [errors] => Array ( [0] => Array ( [domain] => lighthouse [reason] => lighthouseError [message] => Lighthouse returned error: FAILED_DOCUMENT_REQUEST. Lighthouse was unable to reliably load the page you requested. Make sure you are testing the correct URL and that the server is properly responding to all requests. (Details: net::ERR_CONNECTION_FAILED) ) ) [code] => 500 [message] => Lighthouse returned error: FAILED_DOCUMENT_REQUEST. Lighthouse was unable to reliably load the page you requested. Make sure you are testing the correct URL and that the server is properly responding to all requests. (Details: net::ERR_CONNECTION_FAILED) ) )
我尝试的内容:
$url
的值更改为https://developers.google.com
时,我得到了正确的JSON响应。 curl https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url={http://123.456.78.90/webproject/test/some.html}&screenshot=true&key=**********
,但仍然获得 sn@ubuntu:~$ curl https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url={http://123.456.78.90/webproject/test/some.html}&screenshot=true&key=AIzaSyCvLhgU6dByk_eDRV3FCg4g66oVONau2-8
[1] 3987
[2] 3988
sn@ubuntu:~$ {
"error": {
"errors": [
{
"domain": "lighthouse",
"reason": "lighthouseError",
"message": "Lighthouse returned error: FAILED_DOCUMENT_REQUEST.
Lighthouse was unable to reliably load the
page you requested. Make sure you are testing the correct URL and
that the server is properly responding to all requests. (Details:
net::ERR_CONNECTION_FAILED)"
}
],
"code": 500,
"message": "Lighthouse returned error: FAILED_DOCUMENT_REQUEST.
Lighthouse was unable to reliably load the
page you requested. Make sure you are testing the correct URL and
that the server is properly responding to all requests. (Details:
net::ERR_CONNECTION_FAILED)"
}
}