php - Google API - 使用 PHP 的 URL 缩短器

标签 php url curl qr-code

我的代码如下。 URL 缩短服务有效,但当我插入 $POST 时它不起作用。有谁知道如何通过查看代码来解决这个问题?

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

以上方法都可以。

关于php - Google API - 使用 PHP 的 URL 缩短器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13066919/

相关文章:

javascript - 使用 Angular js 和 php 的搜索功能无法正常工作

java - 用于过滤/匹配 URL 的基本开源 java 包是什么?

c++ - CPP : Using UTF-8 in cURL

php - 使用 PHP 从 URL 保存图像(保存为 0KB 文件大小)

php - 在 Mongodb 中聚合嵌套数组

php - mysql 中的时间未正确更新

php - 如何计算两个时间戳之间午夜前后的小时数

android - 如何获取 HTTPResponse 的 URL

python - 在 Django 中创建随机 URL?

php - 如何显示自定义错误,而不是显示简单的html dom.php错误