如何使用linkedin和guzzle?

时间:2016-06-11 13:43:29

标签: php laravel laravel-5.1 guzzle linkedin-api

如何使用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。

1 个答案:

答案 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())解码了使其可读的响应。

希望如果其他人遇到同样的问题会有所帮助。