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
When trying to read the getBody() of the response I get this output
I'm using guzzle "guzzlehttp/guzzle": "^6.4"
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));
Related
I'm using Guzzle 7 to grab content from an external API with basic authentication. It works fine. Now I'd like to integrate cache management. So i've tried to use this plugin: [Guzzle-cache-middleware][1] and I can't make it working correctly. I can get API response and my desired content but the cache directory is not populated.
I've searched all around the web but I can't figure this out. Could you please tell me how to solve this? Here is my code:
$userName = "xxxxxxx";
$password = "yyyyyyyyyyy";
require_once './vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Kevinrob\GuzzleCache\CacheMiddleware;
use Kevinrob\GuzzleCache\Strategy\PublicCacheStrategy;
$stack = HandlerStack::create();
$cache = new CacheMiddleware();
$stack->push($cache, '/home/xxxx/xxxxx/guzzle2/cache');
$client = new Client([
'handler' => $stack,
'base_uri' => 'https://api.xxxxx.com/xxx/',
"timeout" => 30.0,
]);
$json = $client->get('zzzzzz.json', [
'auth' => [
$userName,
$password
]
]);
var_dump($json->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO));
Output:
string(4) "MISS"
So API result is different from cache. But headers params (ETag and Last-Modified) are still unchanged and my cache folder is still blank.
Thank you for your help!
Im passing in a job Id that dynamically needs to populate the uri in guzzle 7 here is the client:
$client = new GuzzleHttp\Client([
'base_uri' => 'https://portal.2nformserver.com/arcgis/rest/services/getLocalRefData/GPServer/Get%20Local%20Reference%20Data/',
['headers' => [
'Accept' =>'application/json'
]]
]);
here is the promise:
$promise = $client->requestAsync('GET', 'jobs/', ['query' => [
"f"=>"json"
]]);
I am needing to pass in a $jobId variable into the Uri portion of the request. I have not been able to find a way to do this. I was hoping for something as easy as:
$promise = $client->requestAsync('GET', 'jobs/{$jobId}', ['query' => [
"f"=>"json"
]]);
or to be able to add something to the array I could pass in but have come up with no answers any help would be greatly appreciated.
you can pass variable to uri using php string format:
$promise = $client->requestAsync('GET', "jobs/$jobId", ['query' => [
"f"=>"json"
]]);
Here's my code below.
When I make a successful authorization, $this->token gets empty value.
When I try this function with PHP Curl, It works well with no problem.
What could be the problem?
By the way, If I try wrong password or username, It gives error.
$client = new \GuzzleHttp\Client();
$response = $client->get($this->url . "/auth/getToken", [
'auth' => [
'myuser',
'mypassword'
]
]);
$this->token = $response;
Based on this code example, $this->token will be an instance of Psr\Http\Message\ResponseInterface whose implementation will contain arrays, integers and a stream resource. Depending on how it is utilized in subsequent code it may appear to be an "empty value" based on how PHP itself tries to cast it.
If we assume that the entire contents of the response body, is the desired authentication code (not well formatted json), we can modify the example to be:
$client = new \GuzzleHttp\Client();
$response = $client->get($this->url . "/auth/getToken", [
'auth' => [
'myuser',
'mypassword'
]
]);
$this->token = $response->getBody()->__toString();
// the __toString() is written to make PHP's casting explicit
or:
$client = new \GuzzleHttp\Client();
$this->token = $client->get($this->url . "/auth/getToken", [
'auth' => [
'myuser',
'mypassword'
]
])
->getBody()
->__toString();
// the __toString() is written to make PHP's casting explicit
I am trying to make a http post request in laravel as below
$client = new Client(['debug'=>true,'exceptions'=>false]);
$res = $client->request('POST', 'http://www.myservice.com/find_provider.php', [
'form_params' => [
'street'=> 'test',
'apt'=> '',
'zip'=> 'test',
'phone'=> 'test',
]
]);
It return empty response. On debugging ,following exception is occurring
curl_setopt_array(): cannot represent a stream of type Output as a STDIO FILE*
I am using latest version of guzzle.
Any idea how to solve it?.
The request() method is returning a GuzzleHttp\Psr7\Response object.
To get the actual data that is returned by your service you should use:
$data = $res->getBody()->getContents();
Now check what you have in $data and if it corresponds to the expected output.
More information on using Guzzle Reponse object here
I had to do this
$data = $res->getBody()->getContents();<br>
but also change<br>
$client = new \GuzzleHttp\Client(['verify' => false, 'debug' => true]);<br>
to<br>
$client = new \GuzzleHttp\Client(['verify' => false]);
Here is what i did for my SMS api
use Illuminate\Support\Facades\Http; // Use this on top
// Sample Code
$response = Http::asForm()
->withToken(env('SMS_AUTH_TOKEN'))
->withOptions([
'debug' => fopen('php://stderr', 'w') // Update This Line
])
->withHeaders([
'Cache-Control' => 'no-cache',
'Content-Type' => 'application/x-www-form-urlencoded',
])
->post($apiUrl,$request->except('_token'));
I create web application that will work with Google Calendar.
I use Guzzle for http requests.
I successfully authorized and got token.
I have some trouble when i tried to add event in some calendar.
$url = 'https://www.googleapis.com/calendar/v3/calendars/'. $calendar_id .'/events';
$client = new Guzzle\Http\Client();
$data = json_encode(array(
"end" => array("date" => "2015-04-02"),
"start" => array("date" => "2015-04-01"),
"summary" => "test3"
));
$request = $client->post($url, [], $data);
$request->setHeader('Authorization', $token_type . ' ' . $token);
$response = $request->send();
echo $response->getBody();
The response is
Client error response [status code] 400 [reason phrase] Bad Request [url] https://www.googleapis.com/calendar/v3/calendars/some_calendar/events
Please explain me what is wrong?
Thanks a lot!
I think you meant to do
use Guzzle\Http\Client;
$url = 'https://www.googleapis.com/calendar/v3/calendars/'. $calendar_id .'/events';
$client = new GuzzleClient($url);