Curl Response handling with Guzzle 6.2 with Laravel 5.3 - php

I am trying to obtain a JSON response from an end point using Guzzle 6.2 with Laravel 5.3.
I am using the following code to make a get request:
$client = new GuzzleHttp\Client([
'base_uri' => 'https://192.xx.xxx.xx6/',
'timeout' => 2.0
]);
$response = $client->request('GET',
'/fineract-provider/api/v1/clients/388?tenantIdentifier=default&pretty=true', [
'verify' => false,
'auth' => ['<username>', '<password>']
]
);
var_dump($response);
Which outputs the following response:
Response {#282
-reasonPhrase: "OK"
-statusCode: 200
-headers: array:7 [
"Server" => array:1 [
0 => "Apache-Coyote/1.1"
]
"Access-Control-Allow-Origin" => array:1 [
0 => "*"
]
"Access-Control-Allow-Methods" => array:1 [
0 => "GET, POST, PUT, DELETE, OPTIONS"
]
"Content-Type" => array:1 [
0 => "application/json"
]
"Transfer-Encoding" => array:1 [
0 => "chunked"
]
"Vary" => array:1 [
0 => "Accept-Encoding"
]
"Date" => array:1 [
0 => "Sat, 04 Feb 2017 15:51:10 GMT"
]
]
-headerNames: array:7 [
"server" => "Server"
"access-control-allow-origin" => "Access-Control-Allow-Origin"
"access-control-allow-methods" => "Access-Control-Allow-Methods"
"content-type" => "Content-Type"
"transfer-encoding" => "Transfer-Encoding"
"vary" => "Vary"
"date" => "Date"
]
-protocol: "1.1"
-stream: Stream {#280
-stream: stream resource #297
wrapper_type: "PHP"
stream_type: "TEMP"
mode: "w+b"
unread_bytes: 0
seekable: true
uri: "php://temp"
options: []
}
-size: null
-seekable: true
-readable: true
-writable: true
-uri: "php://temp"
-customMetadata: []
}
}
But this is not the response I expect. However, when I make the same request in my browser it gives the correct output as below:
What am I doing wrong here?

The Response object contains more information than just the response. You can get the actual output like this:
$output = (string)$response->getBody();
It might be necessary (in certain cases) to cast the result to a string, because the actual result is a stream.
Guzzle documentation: Responses

Related

Symfony 6 | Consume streamed file and send it back to the client

I am developing an administrator website.
There is a section that, when a button is pressed, sends a request to my Symfony backend and the Symfony backend makes an HTTP request to another API that responds with a raw Excel file.
The flow Im trying to make is:
Admin panel [http request] -> Symfony Backend [http request] -> Another API [Generate file and streams it back]
Another API [Streamed Excel file] -> Symfony Backend [Consume the stream] -> Admin panel [Save in device]
I tried doing it like this:
ReportsService
public function reserves_report(String $date1, String $date2): FileForwardResponse {
// This calls to the API to get the streamed file
$guestTypesHttpResponse = $this->httpClient->request(
'GET',
$this->get_endpoint('reports') . "/" . $date1 . "/" . $date2
);
// This class is just to organize things
return new FileForwardResponse(
$guestTypesHttpResponse->getHeaders(),
$guestTypesHttpResponse->getContent(),
);
}
Controller
public function reserves_report(): Response
{
$this->sessionService->singUserIn();
$fileResponse = $this->reportsService->reserves_report($date1, $date2);
return new Response(
$fileResponse->content,
200,
$fileResponse->headers,
);
}
What I was expecting to do is to recive the entire streamed file from the API to the Symfony backend and then use the headers from the API to make it available to download from the client since the headers looks like this:
+headers: array:12 [▶
"connection" => array:1 [▶
0 => "Keep-Alive"
]
"keep-alive" => array:1 [▶
0 => "timeout=5, max=100"
]
"x-ratelimit-limit" => array:1 [▶
0 => "600"
]
"x-ratelimit-remaining" => array:1 [▶
0 => "596"
]
"content-type" => array:1 [▶
0 => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8"
]
"content-disposition" => array:1 [▶
0 => "attachment; filename="filename.xlsx""
]
"expires" => array:1 [▶
0 => "Mon, 26 Jul 1997 05:00:00 GMT"
]
"last-modified" => array:1 [▶
0 => "Sat, 11 Feb 2023 15:51:36"
]
"cache-control" => array:1 [▶
0 => "cache, must-revalidate"
]
"pragma" => array:1 [▶
0 => "public"
]
"transfer-encoding" => array:1 [▶
0 => "chunked"
]
"date" => array:1 [▶
0 => "Sat, 11 Feb 2023 21:51:36 GMT"
]
]
And the response content looks like this:
What I got when I hit my Symfony endpoint is a 0 bytes file named filename.
If I hit the APIs endpoint from the browser directly it downloads it just right but I would like to know if there is any way to do it through Symfony http request.

Google Indexing API calls keep returning 403 codes

I am trying to send vacancies to the Google Indexing API whenever we receive them from our vacancy provider. However, despite having set all the appropriate permissions, I keep receiving 403 status codes.
I have followed the "Prerequisites for the Indexing API" guide and created a Service account. In my case, I have 4 entries for the domain in Google Search Console, so I followed MarcQuay's answer and added the service account to all 4 entries.
I use the following code to implement the Google API Client and make my calls. The method sendGoogleRequest() is called for every vacancy that we receive.
// Google API setup
function sendGoogleRequest(array $aData, int $sStatus = 0)
{
global $DOMAIN, $GOOGLE_AUTH_CONFIG, $GOOGLE_AUTH_SCOPE, $GOOGLE_API_ENDPOINT;
$googleClient = new Google_Client();
$googleClient->useApplicationDefaultCredentials();
$googleClient->addScope([$GOOGLE_AUTH_SCOPE]);
// Update Google Indexing API. This will notify Google that the URL should be crawled again.
$httpClient = $googleClient->authorize();
$endpoint = $GOOGLE_API_ENDPOINT;
$sJobUrl = $DOMAIN . '/vacancies/' . $aData['url'];
$sType = "";
if (!empty($sStatus)) {
switch ($sStatus) {
case 1:
$sType = "URL_UPDATED";
break;
case 2:
$sType = "URL_DELETED";
break;
}
}
$content = "{
\"url\": \"$sJobUrl\",
\"type\": \"$sType\"
}";
$response = $httpClient->post($endpoint, ['body' => $content]);
$status_code = $response->getStatusCode();
return $status_code;
}
I have tried debugging it and it seems that '$credentials' is empty in $googleClient->authorize()
$authHandler = $this->getAuthHandler();
if ($credentials) {
$callback = $this->config['token_callback'];
$http = $authHandler->attachCredentials($http, $credentials, $callback);
} elseif ($token) {
$http = $authHandler->attachToken($http, $token, (array) $scopes);
} elseif ($key = $this->config['developer_key']) {
$http = $authHandler->attachKey($http, $key);
}
return $http;
However I have no idea what could be the cause of this.
Using this code returns a '403' for every call. After having browsed the internet for quite some time now, I can seem to find a definite answer, except for 'make sure the service account is an owner' but as previously stated, this is already the case.
I hope someone can help me out, since I have been stuck on this for longer than I'd like to admit.
If any more information is required I'll gladly update my answer.
EDIT: As per request, here is the full error message return by the $httpClient->post() call.
Response {#268 ▼
-reasonPhrase: "Forbidden"
-statusCode: 403
-headers: array:11 [▼
"Vary" => array:3 [▼
0 => "X-Origin"
1 => "Referer"
2 => "Origin,Accept-Encoding"
]
"Content-Type" => array:1 [▼
0 => "application/json; charset=UTF-8"
]
"Date" => array:1 [▼
0 => "Tue, 24 Sep 2019 11:25:29 GMT"
]
"Server" => array:1 [▼
0 => "ESF"
]
"Cache-Control" => array:1 [▼
0 => "private"
]
"X-XSS-Protection" => array:1 [▼
0 => "0"
]
"X-Frame-Options" => array:1 [▼
0 => "SAMEORIGIN"
]
"X-Content-Type-Options" => array:1 [▼
0 => "nosniff"
]
"Alt-Svc" => array:1 [▼
0 => "quic=":443"; ma=2592000; v="46,43,39""
]
"Accept-Ranges" => array:1 [▼
0 => "none"
]
"Transfer-Encoding" => array:1 [▼
0 => "chunked"
]
]
-headerNames: array:11 [▼
"vary" => "Vary"
"content-type" => "Content-Type"
"date" => "Date"
"server" => "Server"
"cache-control" => "Cache-Control"
"x-xss-protection" => "X-XSS-Protection"
"x-frame-options" => "X-Frame-Options"
"x-content-type-options" => "X-Content-Type-Options"
"alt-svc" => "Alt-Svc"
"accept-ranges" => "Accept-Ranges"
"transfer-encoding" => "Transfer-Encoding"
]
-protocol: "1.1"
-stream: Stream {#256 ▼
-stream: stream resource #123 ▼
wrapper_type: "PHP"
stream_type: "TEMP"
mode: "w+b"
unread_bytes: 0
seekable: true
uri: "php://temp"
options: []
}
-size: null
-seekable: true
-readable: true
-writable: true
-uri: "php://temp"
-customMetadata: []
}
}
I ended up fixing this issue by using a Service account as AuthConfig
// Initialize Google_Client to submit vacancies to Indexing API
$googleClient = new Google_Client();
$googleClient->setAuthConfig($GOOGLE_AUTH_CONFIG);
$googleClient->addScope([$GOOGLE_AUTH_SCOPE]);
$googleClient->setAccessType("offline");
$googleClient is used for every call
function sendGoogleRequest(array $aData, int $sStatus = 0, Google_Client $google_Client) {
global $DOMAIN;
$sJobUrl = 'https://MY-DOMAIN.com/' . $aData['url'];
$sType = "";
if (!empty($sStatus)) {
switch ($sStatus) {
case 1:
$sType = "URL_UPDATED";
break;
case 2:
$sType = "URL_DELETED";
break;
}
}
$urlNotification = new Google_Service_Indexing_UrlNotification();
$urlNotification->setType($sType);
$urlNotification->setUrl($sJobUrl);
$indexingService = new Google_Service_Indexing($google_Client);
$response = $indexingService->urlNotifications->publish($urlNotification);
return $response;
}
This script is then called once per day and posts every URL to the Indexing API. To not exceed the limit of 250 requests per day, make sure you exclude vacancies that should no longer be indexed.

How to read a json_decode result and dump it?

i have a little problem with my json response.
I dumped it in my php code, and the result is what i attached here
How can I dump the statusText ?
I've already tried to decode it, and i've tried this too:
dump($myVar['statusText']);
or something like that
(to obtain the dump of my json i've posted, i've just did
dump($myVar); )
JsonResponse {#325
#data: "{"code":"OK","status":"ok","data":{"UUID":"f239ae18-98af-4224-8b4f-7713c71a5576","order":{something here },"orderRows":[something else here}}"
#callback: null
#encodingOptions: 271
+headers: ResponseHeaderBag {#326
#computedCacheControl: array:2 [
"no-cache" => true
"private" => true
]
#cookies: []
#headerNames: array:4 [
"content-type" => "Content-Type"
"access-control-allow-origin" => "Access-Control-Allow-Origin"
"cache-control" => "Cache-Control"
"date" => "Date"
]
#headers: array:4 [
"content-type" => array:1 [
0 => "application/json; charset=utf-8"
]
"access-control-allow-origin" => array:1 [
0 => "*"
]
"cache-control" => array:1 [
0 => "no-cache, private"
]
"date" => array:1 [
0 => "Mon, 06 May 2019 08:15:28 GMT"
]
]
#cacheControl: []
}
#content: "{"code":"OK","status":"ok","data":{"UUID":"f239ae18-98af-4224-8b4f-7713c71a5576","order":{something},"orderRows":[something else}}"
#version: "1.0"
#statusCode: 200
#statusText: "OK"
#charset: null
}
Just wanna see
"status" => 'ok' and my life would be perfect :D
Looking at this output, it looks like the code is stored in the content also.
"status":"ok"
For this reason, following this documentation on the getData() method, you should be able to retrieve the status:
$data = $myVar->getData();
var_dump($data->status);
It is expected that this would return a string of "ok".

Guzzle returns stream empty body instead of json body

When I use Postman to make an API call I receive a JSON object..which is what I expected.
However When I make same call with Guzzle like so:
$client = new \GuzzleHttp\Client(['base_uri' => 'https://api.dev/']);
$response = $client->request('GET', 'search', [
'verify' => false,
]);
var_dump($response->getBody()); //null
var_dump($response); //returns below
I get dump below from Guzzle
Response {#304 ▼
-reasonPhrase: "OK"
-statusCode: 200
-headers: array:8 [▼
"Server" => array:1 [▶]
"Content-Type" => array:1 [▼
0 => "application/json"
]
"Transfer-Encoding" => array:1 [▶]
"Connection" => array:1 [▶]
"Cache-Control" => array:1 [▶]
"Date" => array:1 [▶]
"X-RateLimit-Limit" => array:1 [▶]
"X-RateLimit-Remaining" => array:1 [▶]
]
-headerNames: array:8 [▼
"server" => "Server"
"content-type" => "Content-Type"
"transfer-encoding" => "Transfer-Encoding"
"connection" => "Connection"
"cache-control" => "Cache-Control"
"date" => "Date"
"x-ratelimit-limit" => "X-RateLimit-Limit"
"x-ratelimit-remaining" => "X-RateLimit-Remaining"
]
-protocol: "1.1"
-stream: Stream {#302 ▼
-stream: stream resource #15 ▼
wrapper_type: "PHP"
stream_type: "TEMP"
mode: "w+b"
unread_bytes: 0
seekable: true
uri: "php://temp"
options: []
}
-size: null
-seekable: true
-readable: true
-writable: true
-uri: "php://temp"
-customMetadata: []
}
}
getBody() returns a stream. If you want to get all the content at once you can use getContents() method and decode json while at it (if you need to)
$payload = json_decode($response->getBody()->getContents());
Further reading - Guzzle Responses
If $response->getBody()->getContents() doesn't work for you, try:
$response->getBody()->__toString();
As mentioned here, sometimes getContents's stream pointer is already at the end of stream and then it returns empty response, but __toString resets it by default.
Milan's solution worked for me but the response coming from API was malformed.
I had to use stripslashes to convert it to a PHP array:
$response = $response->getBody()->__toString();
$response = stripslashes($response);
$data = json_decode($response, true);

Use Laravel passport authentication in API

I am using Laravel Passport Authentication in my project. I want to use passport authentication only in API so I am trying to generate client_id and client_secret using below code but it returns NULL
I have written this code in my routes/api.php
Route::post('/gen_client', function () {
$http = new GuzzleHttp\Client([
'headers' => [ 'Content-Type' => 'application/json' ]
]);
$response = $http->post(url('/') . '/oauth/clients',
['body' => json_encode(
[
'id' => 'ovais.khan#gmail.com',
'name' => 'Ovais2',
'redirect' => url('/') . '/callback'
]
)
]
);
$response_body = json_decode((string)$response->getBody(), true);
var_dump($response_body);
});
Suppose if I have generated client_id using command line php artisan passport:client
now I want to authorized access token but it returns NULL
Route::post('callback', function (Request $request) {
$http = new GuzzleHttp\Client();
$oauth_client = DB::table('oauth_clients')->where('id', '=', 'ovais.khan#gmail.com')->first();
$response = $http->post(url('/') . '/oauth/token', [
'form_params' => [
'grant_type' => 'authorization_code',
'client_id' => $oauth_client->id,
'client_secret' => $oauth_client->secret,
'redirect_uri' => url('/') . '/callback',
'code' => $request->code,
],
]);
$response_body = json_decode((string)$response->getBody(), true);
var_dump($response_body);
$access_token = $response_body['access_token'] ;
$refresh_token = $response_body['refresh_token'];
});
dd(url('/') . '/oauth/clients'); send me the output:
http://localhost/project/public/oauth/clients
dd($response); send me the output:
Response {#224
-reasonPhrase: "OK"
-statusCode: 200
-headers: array:7 [
"Date" => array:1 [
0 => "Sat, 29 Oct 2016 10:19:24 GMT"
]
"Server" => array:1 [
0 => "Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/7.0.9"
]
"X-Powered-By" => array:1 [
0 => "PHP/7.0.9"
]
"Cache-Control" => array:1 [
0 => "no-cache"
]
"Set-Cookie" => array:1 [
0 => "laravel_session=eyJpdiI6Ikx5WXJpTEFvZ0ZiNFwvbFwvYWZRQWxEZz09IiwidmFsdWUiOiJVM29lbTNYSnZwdld5ZHdVRk1IK1hJbG9RenNQa1owS1lRMEFYQ3lVamluV3JcL2RwaVQyRkpsZzkwQ082OU94QkJiSUpGQTZpeTMxWjdpMEtCZ1Byc3c9PSIsIm1hYyI6IjQwNTcwZTQ4YzQ2OGIwODQ5Y2NjMzBiMmIyNmI5MTVkNTY0ZjI0OGQ1Y2M1NTVjYTljNmU4Mjk2Nzg0Yjk2MmMifQ%3D%3D; expires=Sat, 29-Oct-2016 12:19:24 GMT; Max-Age=7200; path=/; HttpOnly"
]
"Content-Length" => array:1 [
0 => "4709"
]
"Content-Type" => array:1 [
0 => "text/html; charset=UTF-8"
]
]
-headerNames: array:7 [
"date" => "Date"
"server" => "Server"
"x-powered-by" => "X-Powered-By"
"cache-control" => "Cache-Control"
"set-cookie" => "Set-Cookie"
"content-length" => "Content-Length"
"content-type" => "Content-Type"
]
-protocol: "1.1"
-stream: Stream {#222
-stream: stream resource #234
wrapper_type: "PHP"
stream_type: "TEMP"
mode: "w+b"
unread_bytes: 0
seekable: true
uri: "php://temp"
options: []
}
-size: null
-seekable: true
-readable: true
-writable: true
-uri: "php://temp"
-customMetadata: []
}
}
Can anyone help me out?
Waiting for positive response.
My teamlead has been resolved this problem. Please check the below link:
Laravel's 5.3 passport and api routes
The reason you are getting NULL for $response_body is when you are setting the variable you are type hinting $response->getBody() as a string, what you are actually getting is an instance of Stream. Also you're trying to json_decode a class, not a json string. $response_body = json_decode((string)$response->getBody(), true); If you would like a JSON response you can do $response_body = $response->json();
For more information these sections of the GuzzleHttp docs will be really useful:
http://guzzle3.readthedocs.io/http-client/response.html#response-body and http://guzzle3.readthedocs.io/http-client/entity-bodies.html

Categories