Search code examples
phplaravellaravel-5.1guzzlelinkedin-api

How to use linkedin and guzzle?


How do I make calls to the linkedin api using guzzle? So far I've tried

    $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);

but I just get an error that reads:

Client error: POST https://api.linkedin.com/v1/people/~?format=json resulted in a 405

Im working with laravel 5.1 and guzzle 6.2.


Solution

  • Silly mistake simply had to change $client->request('POST', from POST to GET so it would be:

    $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);
    

    I also decoded the response which makes it readable by using json_decode($request->getBody()).

    Hope it helps if someone else gets stuck on the same problem.