Google API-PHP的URL缩短器 [英] Google API - URL Shortener with PHP

查看:81
本文介绍了Google API-PHP的URL缩短器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下。 URL缩短服务有效,但是当我插入 $ POST 时无效。有人知道如何解决此问题吗?

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;


推荐答案

尝试如下

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

上面的方法可行。

这篇关于Google API-PHP的URL缩短器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆