我在 PHP 代码中使用 Google Url Shortener 服务。
它正在工作,但我想提高性能,并且我已经看到文档(rif. https://developers.google.com/url-shortener/v1/performance)建议使用部分资源并使用 gzip。
我采用了部分资源方法并且工作正常,但现在我也想使用 gzip 建议。
现在我的代码是
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 代码中转换它......
有什么建议么?
非常感谢您提前!!!
切萨雷