Jakub Kohout
Jakub Kohout

Reputation: 1944

Laravel Guzzle doesn't work but Curl does

I'm using Guzzle for working with external API.

I'm using it this way:

$client = new Client;
$request = $client->request('GET', 'https://api.callrail.com/v1/companies.json', [
    'headers' => [
        'Authorization' => 'Token token="my_api_string"'
    ]
]);

return dd($request);

this is the output

Stream {#255 ▼
  -stream: stream resource @280 ▶}
  -size: null
  -seekable: true
  -readable: true
  -writable: true
  -uri: "php://temp"
  -customMetadata: []
}

but when I use just curl like this

$api_key = 'my_api_string';

$ch = curl_init($api_url);

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Token token=\"{$api_key}\""));

$json_data = curl_exec($ch);

return dd($json_data);

the output looks as expected

{"page":1,"per_page":100,"total_pages":1,"total_records":11,
....
....

what am I doing wrong with the Guzzle?

Upvotes: 3

Views: 1738

Answers (1)

James
James

Reputation: 16359

You have set up the Guzzle request correctly, you just need to do more with the $request after you retrieve it.

Add this line in after your request:

$result = json_decode($request->getBody());

Adding it in to your code it would look like this:

$client = new Client;
$request = $client->request('GET', 'https://api.callrail.com/v1/companies.json', [
    'headers' => [
        'Authorization' => 'Token token="my_api_string"'
    ]
]);

$result = json_decode($request->getBody());

return dd($result);

Upvotes: 8

Related Questions