0

I'm using Google Url Shortener service in a PHP code.

It's working but I'd like to improve performances and I've seen that the documentation (rif. https://developers.google.com/url-shortener/v1/performance) suggests working with partial resources and using gzip.

I've adopted the partial resources approach and works fine but now I'd like to use the gzip suggestion too.

By now my code is

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'];
 }

The Google documentation says to use

Accept-Encoding: gzip
User-Agent: my program (gzip)

My problem is that I don't know how to convert this in my PHP code ......

Any suggestions?

Thank you very much in advance!!!

Cesare

Cesare
  • 1,629
  • 9
  • 30
  • 72
  • 1
    How much data are you receiving from the Google service? If it's not much, it could be that unzipping the zipped response might make take more time than receiving a few kilobytes more data. – Pekka Jan 16 '16 at 15:51
  • The data could be rather small but I need to invoke my function several times in a while cicle and collect all the responses in a string .... I was hoping to save some time. Do you mean that if the single response it's not so big could be the same? Thank you! – Cesare Jan 16 '16 at 16:11
  • It seems unlikely that compressing the response is going to save you a lot of time. But it may be worth trying out and measuring it, one never knows. – Pekka Jan 16 '16 at 17:39
  • 1
    With curl, this seems to be very easy: http://stackoverflow.com/questions/310650/decode-gzipped-web-page-retrieved-via-curl-in-php – Pekka Jan 16 '16 at 17:40

0 Answers0