Google API - URL Shortener with PHP

2019-03-11 00:07发布

My code is below. The URL shortening service works, but it doesn't when I insert my $POST. Does anyone know how to fix this my looking at the code?

// This is the URL you want to shorten
$longUrl = 'http://www.mysite.com/XXXXX/XX/$_POST['qrname']';

// Get API key from : http://code.google.com/apis/console/
$apiKey = 'MyAPIKey';

$postData = array('longUrl' => $longUrl, 'key' => $apiKey);
$jsonData = json_encode($postData);

$curlObj = curl_init();

curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
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);

// Change the response json string to object
$json = json_decode($response);

curl_close($curlObj);

echo 'Shortened URL is: '.$json->id;

8条回答
老娘就宠你
2楼-- · 2019-03-11 00:32

Try with this code. I is working for me.

$api_key = 'YOUR_KEY';
$request_data = array(
    'longUrl' => 'YOUR_LONG_URL'
);

$curl_obj = curl_init(sprintf('%s/url?key=%s', 'https://www.googleapis.com/urlshortener/v1', $api_key));
curl_setopt($curl_obj, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_obj, CURLOPT_POST, true);
curl_setopt($curl_obj, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($curl_obj, CURLOPT_POSTFIELDS, json_encode($request_data));
curl_setopt($curl_obj, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_obj, CURLOPT_SSL_VERIFYHOST, false);

$response = curl_exec($curl_obj);
$json = json_decode($response);
curl_close($curl_obj);

var_dump($json);
die();
查看更多
混吃等死
3楼-- · 2019-03-11 00:34
$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;
    }   
查看更多
女痞
4楼-- · 2019-03-11 00:37

Try as below

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

The above will work.

查看更多
三岁会撩人
5楼-- · 2019-03-11 00:38

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'];
查看更多
男人必须洒脱
6楼-- · 2019-03-11 00:42

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

with the following

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

查看更多
再贱就再见
7楼-- · 2019-03-11 00:44

You have a key, but you are not using it correctly

You should append it to the url, don't send the key in the post

https://www.googleapis.com/urlshortener/v1/url?key='.$apiKey

Please check https://developers.google.com/url-shortener/v1/url/insert

查看更多
登录 后发表回答