Curl 使用PHP中的Google Url Shortener服务:如何使用gzip提高性能?
我正在PHP代码中使用Google Url Shortener服务 这是可行的,但我想提高性能,我看到文档(rif.)建议使用部分资源和gzip 我采用了部分资源的方法,效果很好,但现在我也想使用gzip建议 现在我的代码是Curl 使用PHP中的Google Url Shortener服务:如何使用gzip提高性能?,curl,gzip,google-url-shortener,Curl,Gzip,Google Url Shortener,我正在PHP代码中使用Google Url Shortener服务 这是可行的,但我想提高性能,我看到文档(rif.)建议使用部分资源和gzip 我采用了部分资源的方法,效果很好,但现在我也想使用gzip建议 现在我的代码是 function CompactUrl($longUrl) { $apiKey = API; $postData = array('longUrl' => $longUrl); $jsonData = json_encode($postDat
function CompactUrl($longUrl)
{
$apiKey = API;
$postData = array('longUrl' => $longUrl);
$jsonData = json_encode($postData);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?key='.$apiKey'&fields=id);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
$response = curl_exec($curlObj);
//print 'Response = '.$response;
// Change the response json string to object
$json = json_decode($response);
curl_close($curlObj);
$shortLink = get_object_vars($json);
return $shortLink['id'];
}
谷歌文档中说要使用
Accept-Encoding: gzip
User-Agent: my program (gzip)
我的问题是我不知道如何在我的PHP代码中转换它
有什么建议吗
提前非常感谢
Cesare您从谷歌服务接收到多少数据?如果不是太多,那么解压压缩后的响应可能比接收几千字节的数据需要更多的时间。数据可能很小,但我需要在一段时间内多次调用我的函数,并将所有响应收集到一个字符串中。。。。我希望能节省一些时间。你的意思是,如果单个响应不是那么大,那么它可能是相同的吗?非常感谢。压缩响应似乎不太可能为您节省大量时间。但这可能值得尝试和测量,谁也不知道。有了旋度,这似乎很容易: