【发布时间】:2017-02-11 14:56:44
【问题描述】:
多年来,我一直在使用 Google 日历 API,并取得了巨大的成功。我使用 curl 使用来自 php 的请求。
我一直在尝试使用联系人 api 来实现相同的目标。目前,我要做的就是为我通过 OAuth2 检索令牌的用户检索联系人列表作为 json。我正在使用下面的简单代码进行测试。
$accessToken = "thebiglongaccesstoken1234";
$userMail = "useremail@somewhere.com";
$requestURL ="https://www.google.com/m8/feeds/contacts/".$userMail."/full?v=3.0&alt=json";
$headers = array("Authorization: OAuth ".$accessToken);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $requestURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$responseArray = json_decode($response, TRUE);
var_dump($responseArray);
print '<br/>Response: <pre>' . print_r($responseArray, true) . '</pre>';
print 'curl Error: <pre>' . print_r(curl_error($ch), true) . '</pre>';
浏览器中的响应是:
空 回复: 卷曲错误:
我可以让这个请求在 google OAuth 2.0 Playground 中工作。
我不想使用图书馆,因为我最终想要做的就是通过电子邮件地址找到用户联系人以获取电话号码。我已经为日历制作了自己的函数库,所以不想添加额外的未使用的东西。它应该只需要几行代码。
我想我只是盯着它太久了,错过了明显的东西。任何帮助表示赞赏。
更新:
我使用 curl_getinfo() 解决了这个问题
数组 ( [网址] => https://www.google.com/m8/feeds/contacts/myemail@gmail.com/full?v=3.0&alt=json [内容类型] => 文本/html;字符集=utf-8 [http_code] => 401 [header_size] => 457 [请求大小] => 208 [文件时间] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [总时间] => 0.1244 [namelookup_time] => 0.000837 [连接时间] => 0.00306 [pretransfer_time] => 0.015428 [大小上传] => 0 [大小下载] => 11875 [速度下载] => 95458 [速度上传] => 0 [下载内容长度] => -1 [上传内容长度] => 0 [starttransfer_time] => 0.123992 [重定向时间] => 0 [certinfo] => 数组 ( )
)
【问题讨论】:
-
您可以在将输出传递给
json_decode之前打印输出吗? Contacts API 通常会给出纯文本错误。响应状态代码也会有所帮助。 -
如果我尝试使用 $response 执行任何操作,例如打印它或 var_dump,我会收到损坏的机器人谷歌屏幕并显示 401 或 404 错误和消息“您的请求中有错误。这就是我们的全部知道。” curl 错误上的 var_dump 显示一个空字符串。
标签: php curl google-api google-contacts-api