curl - PHP Strange code behaviour -


here goo.gl url shortener class. using googl::shorten("http://google.com"). can't understand why returns null. doing wrong?

<?php define('google_api_key', 'aizasybs7wpedisz91p-sjonwokkxqveb1sfpf4'); define('google_endpoint', 'https://www.googleapis.com/urlshortener/v1'); class googl {    static function shorten($longurl)     {         // initialize curl connection         $ch = curl_init(             sprintf('%s/url?key=%s', google_endpoint, google_api_key)         );          // tell curl return data rather outputting         curl_setopt($ch, curlopt_returntransfer, true);          // create data encoded json         $requestdata = array(             'longurl' => $longurl         );          // change request type post         curl_setopt($ch, curlopt_post, true);          // set form content type json data         curl_setopt($ch, curlopt_httpheader, array('content-type: application/json'));          // set post body encoded json data         curl_setopt($ch, curlopt_postfields, json_encode($requestdata));          // perform request         $result = curl_exec($ch);         curl_close($ch);          // decode , return json response         return json_decode($result, true);     }  }  ?> 

you need add param.. (since googleapis run on https)

    curl_setopt($ch, curlopt_ssl_verifypeer, false); 

and call

$res= googl::shorten('http://stackoverflow.com'); var_dump($res); 

output :

array (size=3)   'kind' => string 'urlshortener#url' (length=16)   'id' => string 'http://goo.gl/vmnf' (length=18)   'longurl' => string 'http://stackoverflow.com/' (length=25) 

the whole code..

<?php define('google_api_key', 'aizasybs7wpedisz91p-sjonwokkxqveb1sfpf4'); define('google_endpoint', 'https://www.googleapis.com/urlshortener/v1'); class googl {      static function shorten($longurl)     {         // initialize curl connection         $ch = curl_init(             sprintf('%s/url?key=%s', google_endpoint, google_api_key)         );          // tell curl return data rather outputting         curl_setopt($ch, curlopt_returntransfer, true);          // create data encoded json         $requestdata = array(             'longurl' => $longurl         );          // change request type post         curl_setopt($ch, curlopt_post, true);          // set form content type json data         curl_setopt($ch, curlopt_httpheader, array('content-type: application/json'));          curl_setopt($ch, curlopt_ssl_verifypeer, false);          // set post body encoded json data         curl_setopt($ch, curlopt_postfields, json_encode($requestdata));          // perform request         $result = curl_exec($ch);         curl_close($ch);          // decode , return json response         return json_decode($result, true);     }  } $res= googl::shorten('http://stackoverflow.com'); var_dump($res); 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -