how to get access_token from content response laravel - php

I have this response content from api response:
{"token_type":"Bearer","expires_in":31535919,"access_token":"eyJ0eXa99e760ef4e468d17198f9fa6be9d67c36240a9bf9bb74d76b18e18ca2661706e33d1bc80cdAiOiJKV1QiLCJhbGciOiJSU.......
How I can get the access_token value? So I can use it.

You can use json_decode function to conver that into object and access it there.
$response = '{"token_type":"Bearer","expires_in":31535919,"access_token":"eyJ0eXa99e760ef4e468d17198f9fa6be9d67c36240a9bf9bb74d76b18e18ca2661706e33d1bc80cdAiOiJKV1QiLCJhbGciOiJSU.......';
$response = json_decode($response, true);
echo $response['access_token'];
Hope this helps.

Related

How to parse API response correctly

My Problem is relatively simple:
I make an API call and get the following answer in response:
[{"RowKey":24764}]
The Content-Type I'm receiving is text/html
Somehow, I'm just not able to parse this correctly. neither json_encodeor json_decodeseems to help.
I'm trying to Map the object into my response class:
class ApiResponse {
public $schedules = [];
}
with the json object mapper from: https://github.com/mintware-de/json-object-mapper
Consider this test case:
$response = '[{"RowKey":24764}]';
$result = json_decode($response, true);
echo $result[0]['RowKey']; // Output: 24764

Storing response from PHP Guzzle as JSON in database (Laravel)

Im calling a vehicle data API at an endpoint and can return the data fine using:
$client = new Client();
$url = "https://uk1.ukvehicledata.co.uk/api/MISC DETAILS;
$result = $client->get($url);
return $result->request;
This is using mock data so the response is:
{
"Request":{
"RequestGuid":"",
"PackageId":""
"Response":{
// VEHICLE DATA
}
}
However, I now wish to store the response in the database but cannot access ->request or ->Request etc.
Using:
return json_encode( (array)$result );
Actually returns the headers from Guzzle and no response data.
Any help?
You have to convert the body into JSON first:
$jsonArray = json_decode($result->getBody()->getContents(), true);
echo $jsonArray['Request']['RequestGuid'];

Accessing a URL stored in a variable from a controller

I am in a confusing situation
In my Laravel controller, I have a variable
public function storeName($key)
$store = new Store();
$storeName = $store->connectAPI($key);
This $storeName variable will actually give me a URL, which if accessed, will give me a JSON response.
If I die and dump $storeName variable it will print
http://store123.com?key=2093983892
But, what I actually want is to access this $storeName variable, by passing a GET request in my controller, so I can get a JSON response from this API call.
How can I access this URL in my controller?
From Guzzle docs
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://api.github.com/user', []);
if($res->getStatusCode())
{
// "200"
$json = $res->getBody();
}
I used json_decode inside a curl_init function to solve this issue.

$response->getBody()->getContents() returning empty string

I have the code below:
<?php
use Zend\Diactoros\Response;
$response = new Response('This is the response content');
echo $response->getBody()->getContents();
echo $response->getBody();
I'm passing the body directly in the constructor.
I'm trying to get the body of this response, nothing more then it, but when i call the getBody() or the getBody()->getContents() it's give me a empty string.
I've tried another alternative that works:
<?php
use Zend\Diactoros\Response;
$response = new Response;
$response->getBody()->write('This is the response content');
echo $response->getBody()->getContents();
echo $response->getBody();
and it outputs:
This is the response content This is the response content
Why the first and short form isn't working?
I found the problem, that's was my fault.
Actually, the Response __constructor gets a StreamInterface as first parameter, not a string.
The StreamInterface implementation is where you have to write your body, other wise, you get no response.
Here is the good approach:
$stream = new Stream('php://temp', 'rw');
$stream->write('This is a response');
$response = (new Response($stream));
echo $response->getBody();

Accessing specific properties from Guzzle response?

I'm using Guzzle to get an HTTP response. If I do this:
$response = $res->getBody();
I get an object with 'email' as one of the properties. But if I do either:
$email = $res->getBody()->email;
or
$email = $response->email
I get a 'No value for email' error. What am I missing?? How can I access a specific property in the response object?
The getBody method returns an instance of StreamInterface. You first need to retrieve the contents of the response:
$response = (string) $res->getBody();
Only then you can decode the json payload:
$json = json_decode($response);
$email = $json->email;

Categories