Laravel Google Contacts API调用返回响应ok,但JSON为空
目前正在构建一个Laravel应用程序,该应用程序使用Socialite软件包成功验证使用Google凭据的用户。然而,我试图向Google服务器发出GET请求以检索给定用户的联系人列表,我在Google oAuth 2游乐场上进行了一些试验,并试图在我的应用程序中模拟相同的请求。我创建了以下函数:Laravel Google Contacts API调用返回响应ok,但JSON为空,laravel,google-api,google-contacts-api,guzzle,laravel-socialite,Laravel,Google Api,Google Contacts Api,Guzzle,Laravel Socialite,目前正在构建一个Laravel应用程序,该应用程序使用Socialite软件包成功验证使用Google凭据的用户。然而,我试图向Google服务器发出GET请求以检索给定用户的联系人列表,我在Google oAuth 2游乐场上进行了一些试验,并试图在我的应用程序中模拟相同的请求。我创建了以下函数: public function getContactList() { $client = new \GuzzleHttp\Client(); $email = \Auth::user()->
public function getContactList()
{
$client = new \GuzzleHttp\Client();
$email = \Auth::user()->email;
$token = \Session::get('token');
$json = $client->get('https://www.google.com/m8/feeds/contacts/default/full/', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
]);
dd($json);
return $json;
}
经过无休止的努力,我终于得到了一个肯定的回答,但是没有用,身体里没有任何东西,用Json_decode解码会得到null,下面是回答:
Response {#198 ▼
-reasonPhrase: "OK"
-statusCode: 200
-effectiveUrl: "https://www.google.com/m8/feeds/contacts/default/full/"
-headers: array:11 [▼
"expires" => array:1 [▼
0 => "Mon, 30 Mar 2015 15:19:52 GMT"
]
"date" => array:1 [▼
0 => "Mon, 30 Mar 2015 15:19:52 GMT"
]
"cache-control" => array:1 [▶]
"vary" => array:2 [▶]
"content-type" => array:1 [▶]
"x-content-type-options" => array:1 [▶]
"x-frame-options" => array:1 [▶]
"x-xss-protection" => array:1 [▶]
"content-length" => array:1 [▶]
"server" => array:1 [▶]
"alternate-protocol" => array:1 [▶]
]
-headerNames: array:11 [▼
"expires" => "Expires"
"date" => "Date"
"cache-control" => "Cache-Control"
"vary" => "Vary"
"content-type" => "Content-Type"
"x-content-type-options" => "X-Content-Type-Options"
"x-frame-options" => "X-Frame-Options"
"x-xss-protection" => "X-XSS-Protection"
"content-length" => "Content-Length"
"server" => "Server"
"alternate-protocol" => "Alternate-Protocol"
]
-body: Stream {#197 ▼
-stream: :stream {@8 ▼
wrapper_type: "PHP"
stream_type: "TEMP"
mode: "w+b"
unread_bytes: 0
seekable: true
uri: "php://temp"
options: []
}
-size: null
-seekable: true
-readable: true
-writable: true
-uri: "php://temp"
-customMetadata: []
}
-protocolVersion: "1.1"
}
我可以更改什么,或者需要更改什么才能获得完整的联系人列表,而不是空的200响应
更新:我做了一些测试来验证我的请求的准确性,发现上面的请求实际上返回了一个ATOM提要,这可能就是问题所在。当我请求驱动返回JSON响应的API时,只要使用JSON_decode对其进行解析,我就可以毫不费力地提取适当的数据。我需要使用哪个函数在PHP中解析ATOM数据才能检索它 你得到了一个来自Guzzle的
响应对象。Response
对象上有一个可用的json
方法,因此您应该能够:
$response = $client->get('https://www.google.com/m8/feeds/contacts/default/full/', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
]);
echo $response->json();
来源:您是否尝试将'alt=json'参数添加到GET请求中?像这样:
$response = $client->get('https://www.google.com/m8/feeds/contacts/default/full?alt=json', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
]);
我一直在尝试用JSON获取contacts API,看起来这是正确的方法:
Atom是基于xml的。我知道Atom是基于xml的,我的问题是:如何将xml/Atom从$json对象体提取到字符串中?如果Atom提要嵌入到json响应中,那么您可以简单地使用PHP的json解码函数