如何使用guzzle拨打linkedin api?到目前为止,我已经尝试了
$client = new \GuzzleHttp\Client(['base_uri' => 'https://api.linkedin.com']);
$access_token = 'the_access_token';
$req = $client->request('POST', '/v1/people/~?format=json', [
'headers' => ["Authorization" => "Bearer " . $access_token,
"Content-Type" => "application/json", "x-li-format"=>"json"],
'client_id' => 'the_client_id',
'client_secret' => 'the_client_secret',
'connection' => 'Keep-Alive'
]);
dd($req);
但我收到的错误是:
Client error: POST https://api.linkedin.com/v1/people/~?format=json resulted in a 405
我正在使用laravel 5.1和guzzle 6.2。
答案 0 :(得分:0)
愚蠢的错误只需将$client->request('POST',
从POST
更改为GET
所以它会是:
$client = new \GuzzleHttp\Client(['base_uri' => 'https://api.linkedin.com']);
$access_token = 'the_access_token';
$req = $client->request('GET', '/v1/people/~?format=json', [
'headers' => ["Authorization" => "Bearer " . $access_token,
"Content-Type" => "application/json", "x-li-format"=>"json"],
'client_id' => 'the_client_id',
'client_secret' => 'the_client_secret',
'connection' => 'Keep-Alive'
]);
dd($req);
我还使用json_decode($request->getBody())
解码了使其可读的响应。
希望如果其他人遇到同样的问题会有所帮助。