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;
Related
I have an API service that requires an encrypted string to be sent as a payload to their service and you in turn get an encrypted string response to be decrypted by another service. The challenge is that I get a string lije this Kcc6rcdO41XJ9ZNavb6AeJXwM0pxEfRLUipPpIM3ayRrLT2tWp4xD/0B28/W09siYJ2dVOZGOA1jUSMZLxXdFZimJHrzxZKuiSvV+ECPY4CyyTi3Sp1CvKuNLERFu4iQ8knlq00Fj2nL8qVyu19WOg== as a response but I cannot pass the string as a payload to the decryption endpoint as it always return null.
This is my code
$response2 = Http::withHeaders($headers2)->post($url2, $fields2);
$json = json_decode($response2);
$fields3 = [
"value" => json_decode($json)
];
$response3 = Http::withHeaders($headers)->post($url3, $fields3);
My $response2 = Kcc6rcdO41XJ9ZNavb6AeJXwM0pxEfRLUipPpIM3ayRrLT2tWp4xD/0B28/W09siYJ2dVOZGOA1jUSMZLxXdFZimJHrzxZKuiSvV+ECPY4CyyTi3Sp1CvKuNLERFu4iQ8knlq00Fj2nL8qVyu19WOg== but it doesn`t end up passing into the new endpoint.
I was able to resolve this by accessing the string using body()
$json = $response2->body();
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.
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'];
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.
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();