How to handle a stream response using the Kubernetes API? - php

When I ssh directly inside my pod, and run the following on the command line: curl -v --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" https://kubernetes.default.svc.cluster.local/apis/batch/v1/watch/namespaces/kludge/jobs/api-job-12 it works and returns all the updates for the specified job (api-job-12) in the form of a stream.
However, when I'm inside my application level code, I can't get the API to stream the response (the request times out with no response at all). I'm working inside a PHP(Laravel) environment and I'm using Guzzle for my http client.
Here's my code:
$token = file_get_contents('/var/run/secrets/kubernetes.io/serviceaccount/token');
$client = new Client([
'headers' => [
'Authorization' => "Bearer {$token}"
]
]);
$response = $client->get(
'https://kubernetes.default.svc/apis/batch/v1/watch/namespaces/kludge/jobs/api-job-12',
[
'verify' => '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt',
'stream' => true
]
);
dd($response->getBody()->getContents());
When I dump $response->getBody() I get the following:
^ GuzzleHttp\Psr7\Stream {#338
-stream: stream resource #21
crypto: array:4 [
"protocol" => "TLSv1.2"
"cipher_name" => "ECDHE-RSA-AES128-GCM-SHA256"
"cipher_bits" => 128
"cipher_version" => "TLSv1.2"
]
timed_out: false
blocked: true
eof: false
wrapper_data: array:4 [
0 => "HTTP/1.1 200 OK"
1 => "Content-Type: application/json"
2 => "Date: Sun, 01 Mar 2020 20:15:33 GMT"
3 => "Connection: close"
]
wrapper_type: "http"
stream_type: "tcp_socket/ssl"
mode: "r"
unread_bytes: 0
seekable: false
uri: "https://kubernetes.default.svc/apis/batch/v1/watch/namespaces/kludge/jobs/api-job-12"
options: array:2 [
"http" => array:5 [
"method" => "GET"
"header" => """
Authorization: Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrbHVkZ2UiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlY3JldC5uYW1lIjoiZGVmYXVsdC10b2tlbi14ZjhsciIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJkZWZhdWx0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiZDA5ZjZiZTAtM2UyOS00NWU3LWI3ZjgtOGE1YWI0OGZjNDJiIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50OmtsdWRnZTpkZWZhdWx0In0.eAtABzj-SimmTGNCQXkmtVHFPYoxiZWV7ET5tYK3OIa6-Ea6WA3cy7cMmObRILTc26cLU4YX8YovhzoNAV8RkteKAVGv2HNvaeOXD_AKkYilX618SUMEfat-zsnXYUego24gNLPtPFRefRyEwAnxf6E61DDwSWWlyptKiggcnl8GHrlY_14oumOsFpsjsRTc807DsuZGn1jCU1Dw2DPhSz457a-afXb0jggzorYNzDtfG6rBTKYctPI4wfh30y9iwjPLTU5L5B-8mYqWn9lgOs2Z9XkFu1GRUD19j6bgAnzoyfVCY8uJp9FGi1Ega84n_MsC6cXmS7K7_QiyBtFR-Q
User-Agent: GuzzleHttp/6.5.1 curl/7.64.0 PHP/7.2.28
Host: kubernetes.default.svc
Content-Length: 0
Connection: close
"""
"protocol_version" => "1.1"
"ignore_errors" => true
"follow_location" => 0
]
"ssl" => array:4 [
"cafile" => "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
It does appear to be a stream object, but the object says it has no bytes to read and that it is not seekable. Does anyone have any ideas as to how I can get a stream response using PHP here?
Here's the Kubernetes API endpoint that I'm calling:
https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#watch-47

Have you tried to actually read from this stream? Private fields can be confusing, so I would suggest to try the public interface and see what you get:
$body = $response->getBody();
while (!$body->eof()) {
echo $body->read(1024);
}

Related

Uploaded file opened using fopen error in guzzle json_encode error: Type is not supported

I cannot attach the uploaded file. I see that fopen($image->getPathname(),'r') returns not convertible json data type making guzzle return error json_encode error: Type is not supported. What am I doing wrong here? Thank you.
stream resource #586 ▼
timed_out: false
blocked: true
eof: false
wrapper_type: "plainfile"
stream_type: "STDIO"
mode: "r"
unread_bytes: 0
seekable: true
uri: "\Temp\php2D41.tmp"
options: []
//use Illuminate\Support\Facades\Http;
//use Illuminate\Support\Facades\File;
if($request->hasFile('imgInp1'))
{
foreach(request('imgInp1') as $image)
{
$message = Http::post('https://graph.facebook.com/v9.0/me/message_attachments?access_token='.$access_token,
[
'headers' => [
'Content-Type' => 'multipart/form-data',
],
'multipart' => [
'name' => 'image',
'image_path' => $image->getPathname(),
'image_mime' => $image->getmimeType(),
'image_org' => $image->getClientOriginalName(),
'contents' => fopen($image->getPathname(), 'r'),
],
'message'=>[
'attachment' => [
'type' => 'image',
'payload' => [
"is_reusable"=> true,
],
],
],
]);
}
}
From the url you used this is the curl request provided in facebook api_guide
curl \
-F 'message={"attachment":{"type":"image", "payload":{"is_reusable":true}}}' \
-F 'filedata=#/tmp/shirt.png;type=image/png' \
"https://graph.facebook.com/v9.0/me/message_attachments?access_token=<PAGE_ACCESS_TOKEN>"
This is what -F means in curl
-F, --form <name=content>
 (HTTP SMTP IMAP) For HTTP protocol family, this lets curl emu‐
late a filled-in form in which a user has pressed the submit
button. This causes curl to POST data using the Content-Type
multipart/form-data according to RFC 2388.
(Source: man curl)
By interpreting second line of curl,
You can also tell curl what Content-Type to use by using
'type=', in a manner similar to:
curl -F "web=#index.html;type=text/html" example.com
or
curl -F "name=daniel;type=text/foo" example.com
So it tells that you need multipart/form-data.
I am using guzzle directly inplace of using HttpClient(comes by default in laravel, comfirm from composer.json). Also I suggest to use try catch block as it is not necessary that it will always be 200 response.
$message["attachment"] = [
"type" => "image",
"payload"=> ["is_reusable"=>true]
];
try{
$client = new \GuzzleHttp\Client();
if(file_exists($image)){ // or can use \Illuminate\Support\Facades\File::exists($image)
$request = $client->post( 'https://graph.facebook.com/v9.0/me/message_attachments?access_token='.$access_token, [
// 'headers' => [], //if you want to add some
'multipart' => [
[
'name' => 'message',
'contents' => json_encode($message),
],
[
'Content-type' => $image->getmimeType(),
'name' => 'filedata',
'contents' => fopen($image->getPathname(), 'r'),
]
]
]);
if ($request->getStatusCode() == 200) {
$response = json_decode($request->getBody(),true);
//perform your action with $response
}
}else{
throw new \Illuminate\Contracts\Filesystem\FileNotFoundException();
}
}catch(\GuzzleHttp\Exception\RequestException $e){
// see this answer for https://stackoverflow.com/questions/17658283/catching-exceptions-from-guzzle/64603614#64603614
// you can catch here 400 response errors and 500 response errors
// see this https://stackoverflow.com/questions/25040436/guzzle-handle-400-bad-request/25040600
}catch(Exception $e){
//catch other errors
}

Underscore in request header in Laravel

why laravel doesn't accept vars with underscore from request header?
I made a simple request example with a variable: "token_auth" with value 123 , but inside in my route doesn't get this value.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class Test extends Controller
{
public function teste(Request $request){
dd($request->header());
}
}
only prints header with no underscore:
array:9 [
"thisheadernounderline" => array:1 [
0 => "312321"
]
"cache-control" => array:1 [
0 => "no-cache"
]
"postman-token" => array:1 [
0 => "3c461fd1-5bea-4100-9926-81c14cb5810c"
]
"user-agent" => array:1 [
0 => "PostmanRuntime/7.1.1"
]
"accept" => array:1 [
0 => "*/*"
]
"host" => array:1 [
0 => "localhost"
]
"cookie" => array:1 [
0 => "XSRF-TOKEN=eyJpdiI6IkJwM3pjVkFBb2hxS2d4MDFcL2srM0h3PT0iLCJ2YWx1ZSI6IiszRzhoTzV0VzN5YUkydUNUTGR5aENVd291ZW01SkZ4V2ZxQkNDTGJwbDlyMFFJZGxzNnorMkF0VUNTbHpoRndLV3FmbndJWFhkXC9cL3IzOGZvN25zN3c9PSIsIm1hYyI6IjQwZWQ1YmJhM2VjM2I3N2RiNWZlYjcwYjZmYzQ0NDk5YjkwZDc4YzRjNGQwZjQxNDVkOGU1NDU0MTA0OWI2YWYifQ%3D%3D; laravel_session=eyJpdiI6IitSckpmOFI1TmpuXC9SSUt2QVY3VlFRPT0iLCJ2YWx1ZSI6IlwvVk1EaDdYdDNxRTZLNytRcnZDTlNiaVlFTWVRVmNUOHlyVnFia0pDeE9HNWpNa3QrWlBsNnNoVEduVkhrMUhkYURoNDI4cW9RdXVHU0lIS0JZN2REQT09IiwibWFjIjoiNWJmYmJmNTdmMzJkZjQ1OGQ4NTM1NjhhMzQxNDk5NWUxOTA5OGVjOThkODkyNDgwZTA2NzEyYjFlZmE2YjVjOSJ9"
]
"accept-encoding" => array:1 [
0 => "gzip, deflate"
]
"connection" => array:1 [
0 => "keep-alive"
]
]
is there any workaround to solve this?
And I cant change this because this variable comes from an API (aready asked to change but they wont wanna change).
Already tried with laravel 5.4 and laravel 5.6.
ps: with simple php works fine (no laravel framework)
This worked for my scenario.
I used this workaround in Laravel to solve my problem:
foreach (getallheaders() as $name => $value) {
echo "$name: $value\n";
}
output:
token_auth: 123
cache-control: no-cache
Postman-Token: f5bf7878-1f64-4ee8-907a-991d73ab8667
User-Agent: PostmanRuntime/7.1.1
Accept: */*
Host: localhost
cookie: XSRF-TOKEN=eyJpdiI6IkJwM3pjVkFBb2hxS2d4MDFcL2srM0h3PT0iLCJ2YWx1ZSI6IiszRzhoTzV0VzN5YUkydUNUTGR5aENVd291ZW01SkZ4V2ZxQkNDTGJwbDlyMFFJZGxzNnorMkF0VUNTbHpoRndLV3FmbndJWFhkXC9cL3IzOGZvN25zN3c9PSIsIm1hYyI6IjQwZWQ1YmJhM2VjM2I3N2RiNWZlYjcwYjZmYzQ0NDk5YjkwZDc4YzRjNGQwZjQxNDVkOGU1NDU0MTA0OWI2YWYifQ%3D%3D; laravel_session=eyJpdiI6IitSckpmOFI1TmpuXC9SSUt2QVY3VlFRPT0iLCJ2YWx1ZSI6IlwvVk1EaDdYdDNxRTZLNytRcnZDTlNiaVlFTWVRVmNUOHlyVnFia0pDeE9HNWpNa3QrWlBsNnNoVEduVkhrMUhkYURoNDI4cW9RdXVHU0lIS0JZN2REQT09IiwibWFjIjoiNWJmYmJmNTdmMzJkZjQ1OGQ4NTM1NjhhMzQxNDk5NWUxOTA5OGVjOThkODkyNDgwZTA2NzEyYjFlZmE2YjVjOSJ9
accept-encoding: gzip, deflate
Connection: keep-alive

Curl Response handling with Guzzle 6.2 with Laravel 5.3

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

Guzzle and Form + Basic Auth issues with Laravel

I have been looking into grabbing some data from Livecoding.tv, and i am currently reusing my Oauth2 controller that i used for the twitch API, which should be pretty straightforward.
If someone does not know, the flow used by Oauth is the following:
Redirect user to third party Oauth link with your app code.
User Authorizes.
User redirected to your website again, with an Authorized token, that you can then post to the third party to get your refresh token etc.
Now on step 3, i am running into some problems. Here is the description and example by the developer:
Getting token: https://www.livecoding.tv/o/token/
Header
A HTTP Basic auth, using the application_code as username, and application_secret as password, as seen in the example below.
POST Body
code=EXAMPLE Token gotten from redirect
grant_type=Your grant type (authorization_type)
redirect_uri=Your redirect URL
And here is an example cURL request from the documentation of a working curl request.
curl -X POST -d "grant_type=authorization_code&code=Php4iJpXGpDT8lCqgBcbfQ2yzhB0Av&client_id=vCk6rNsC&redirect_uri=http://localhost/externalapp" -u"vCk6rNsC:sfMxcHUuNnZ" https://www.livecoding.tv/o/token/
So i tried to make this in Postman(https://www.getpostman.com/), which worked out of the box, i then asked Postman to convert this to PHP, to see if i had missed something. Here is the outcome of the working Postman request:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.livecoding.tv/o/token/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "code=SOMECODE&client_id=SOMECLIENTID&redirect_uri=SOMEURL&grant_type=authorization_code",
CURLOPT_HTTPHEADER => array(
"authorization: Basic U09NRVVTRVI6U09NRVBBU1NXT1JE",
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
"postman-token: c8df4bbc-cbd0-73eb-df35-80210989db33"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
I was pretty happy this worked out right away, so i just needed to make a Guzzle client do the same, which already works for the Twitch Oauth, here is my code for that:
{
$providers = ServiceProvider::findOrFail(2);
$client = new Client([
'base_uri' => $providers->oauth_url . '/token/',
'form_params' => [
'code' => $token,
'grant_type' => 'authorization_code',
'client_id' => $providers->application_code,
'redirect_uri' => $providers->redirection_url
],
'auth' => [
'somestring',
'someotherstring',
],
'headers' => [
'Content type' => 'application/x-www-form-urlencoded',
'cache-control' => 'no-cache'
]
]);
$response = $client->request('POST');
return ($response);
}
This would just return me with a 401, so i decided to do some debugging, and here is the request that comes, if i stop it right before it is sent:
Client {#637 ▼
-config: array:10 [▼
"base_uri" => Uri {#676 ▼
-scheme: "https"
-userInfo: ""
-host: "www.livecoding.tv"
-port: null
-path: "/o/token/"
-query: ""
-fragment: ""
}
"form_params" => array:4 [▼
"code" => "SOMECODE"
"grant_type" => "authorization_code"
"client_id" => "SOMECLIENTID"
"redirect_uri" => "http://REDIRECTURI"
]
"auth" => array:2 [▼
0 => "SOMECLIENTID"
1 => "SOMECLIENTSECRET"
]
"headers" => array:3 [▼
"Content type" => "application/x-www-form-urlencoded"
"cache-control" => "no-cache"
"User-Agent" => "GuzzleHttp/6.2.1 curl/7.26.0 PHP/5.6.27-1~dotdeb+7.1"
]
"handler" => HandlerStack {#664 ▼
-handler: Closure {#671 ▼
class: "GuzzleHttp\Handler\Proxy"
parameters: {▼
$request: {▼
typeHint: "Psr\Http\Message\RequestInterface"
}
$options: {▼
typeHint: "array"
}
}
use: {▼
$default: Closure {#669 ▼
class: "GuzzleHttp\Handler\Proxy"
parameters: {▼
$request: {▼
typeHint: "Psr\Http\Message\RequestInterface"
}
$options: {▼
typeHint: "array"
}
}
use: {▼
$default: CurlMultiHandler {#634 ▼
-factory: CurlFactory {#667 ▼
-handles: []
-maxHandles: 50
}
-selectTimeout: 1
-active: null
-handles: []
-delays: []
}
$sync: CurlHandler {#666 ▼
-factory: CurlFactory {#665 ▼
-handles: []
-maxHandles: 3
}
}
}
file: "/LARAVELPATH/vendor/guzzlehttp/guzzle/src/Handler/Proxy.php"
line: "25 to 29"
}
$streaming: StreamHandler {#670 ▼
-lastHeaders: []
}
}
file: "LARAVELPATH/vendor/guzzlehttp/guzzle/src/Handler/Proxy.php"
line: "49 to 53"
}
-stack: array:4 [▼
0 => array:2 [▼
0 => Closure {#672 ▼
class: "GuzzleHttp\Middleware"
parameters: {▶}
file: "LARAVELPATH/vendor/guzzlehttp/guzzle/src/Middleware.php"
line: "54 to 69"
}
1 => "http_errors"
]
1 => array:2 [▼
0 => Closure {#673 ▼
class: "GuzzleHttp\Middleware"
parameters: {▶}
file: "LARAVELPATH/vendor/guzzlehttp/guzzle/src/Middleware.php"
line: "148 to 150"
}
1 => "allow_redirects"
]
2 => array:2 [▼
0 => Closure {#674 ▼
class: "GuzzleHttp\Middleware"
parameters: {▶}
file: "LARAVELPATH/vendor/guzzlehttp/guzzle/src/Middleware.php"
line: "27 to 43"
}
1 => "cookies"
]
3 => array:2 [▼
0 => Closure {#675 ▼
class: "GuzzleHttp\Middleware"
parameters: {▶}
file: "LARAVELPATH/vendor/guzzlehttp/guzzle/src/Middleware.php"
line: "216 to 218"
}
1 => "prepare_body"
]
]
-cached: null
}
"allow_redirects" => array:5 [▼
"max" => 5
"protocols" => array:2 [▼
0 => "http"
1 => "https"
]
"strict" => false
"referer" => false
"track_redirects" => false
]
"http_errors" => true
"decode_content" => true
"verify" => true
"cookies" => false
]
}
As you can see here, there is just a parameter called 'auth' in here, and not the basic auth that is documented in Guzzle here: http://docs.guzzlephp.org/en/latest/request-options.html#auth
Though i would mention that the correct values are entered.
For the debugging i just did dd($client), i do not know if this will give me all the answers?
So the other possibility is to base64 encode it (like it is done in basic auth in general), and add a "Authorization" header manually, i tried that, but i am unaware if i am doing it correctly when i do:
$credentials = base64_encode($clientvariable . ':' . $clientsecretvariable)
Would that be the correct way? Though i would rather use this as a last resort if i do not get the auth parameter to work in Guzzle.
I am aware of the other Stackoverflow questions about Guzzle and basic auth, and yes i have read them, hopefully i have provided with enough information to show that.
The problem here was related to how i return the response from Guzzle.
Since i just returned $response, which is just the stream, the browser never got anything returned, and since chrome auto refresh, he runs the request twice, and on the second time, i would have just my code twice, which would have returned an error.
I ended up going crazy about this, until i changed browser, and saw that nothing ever happened, which pointed me in the right direction.

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