Google API - URL Shortener with PHP Google API - URL Shortener with PHP curl curl

Google API - URL Shortener with PHP


Try as below

$longUrl = 'http://www.mysite.com/XXXXX/XX/'.$_POST['qrname'];

The above will work.


$longUrl = "http://www.xxxxxxx.com";    $postData = array('longUrl' => $longUrl);    $jsonData = json_encode($postData);    //4    $curlObj = curl_init();     curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?key=yourappkey');    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);    //5    $response = curl_exec($curlObj);    $json = json_decode($response);//       echo "<pre>";//    print_r($json);exit;    //6    curl_close($curlObj);    //7    if(isset($json->error)){        echo $json->error->message;    }else{        echo $json->id;    }   


you are passing the php variable between the single quotes so it will not be parsed.pass it between double quotes like

$longUrl = "http://www.mysite.com/XXXXX/XX/$_POST['qrname']";

OR concatinate like this

$longUrl = 'http://www.mysite.com/XXXXX/XX/'.$_POST['qrname'];