MadzQuestioning
MadzQuestioning

Reputation: 3772

Laravel GuzzleHttp response empty

I'm trying to retrieve the value of a URL but it returns a null response. Not sure what I'm doing wrong I've been trying to retrieve the value but I get an empty value. Below is my code

$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'https://fantasy.premierleague.com/api/bootstrap-static/');

dd($response->getBody()->getContents());

When trying to dump the response I get the below response

enter image description here

When trying to read the getBody() of the response I get this output

enter image description here

I'm using guzzle "guzzlehttp/guzzle": "^6.4"

Upvotes: 0

Views: 3068

Answers (1)

MadzQuestioning
MadzQuestioning

Reputation: 3772

OK I found the solution to my problem. It's got nothing to do with

$response->getBody()->getContents()

But the problem was the Endpoint/URL might require a user agent as part of the parameter of the url

my code I was able to retrieve the value using the code below

$url = 'https://fantasy.premierleague.com/api/bootstrap-static/';
$client = new \GuzzleHttp\Client();

$response = $client->request('GET', $url, [
    'verify' => false,
    'headers' => [
        'User-Agent' => 'CUSTOM_AGENT_YOU_WANT' // THIS IS WHAT I ADDED TO MAKE IT WORK
    ]
]);

dd(json_decode($response->getBody()->getContents(), true));

Upvotes: 3

Related Questions