【发布时间】:2014-07-13 05:51:56
【问题描述】:
我正在使用 Google URL Shortener API 制作应用程序。
在 MackBook Pro 上,下面的代码可以工作,但在 Windows PC 上,它不工作,屏幕上只显示“NULL”。
<?php
// APIkey
$apiKey = '[Here is my API KEY]';
$longUrl = "http://www.absolute-keitarou.net/blog/";
$url1 = 'https://www.googleapis.com/urlshortener/v1/url?key='.$apiKey;
$params = json_encode(array(
"longUrl" => $longUrl
));
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$res = json_decode(curl_exec($curl));
curl_close($curl);
var_dump($res);
?>
在 MacBook 上,代码可以正常工作并显示以下内容。
object(stdClass)#1 (3) { ["kind"]=> string(16) "urlshortener#url" ["id"]=> string(19) "http://goo.gl/TMII0" ["longUrl"] => 字符串(38)“http://www.absolute-keitarou.net/blog/”}
我希望 Windows PC 显示与 MacBook 相同的输出。请告诉我该怎么做。
【问题讨论】:
-
添加'curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);'似乎有帮助。此外,将结果“$res”解码为单独的步骤。这使您可以阅读响应。
标签: php curl google-url-shortener