i have a problem dedoding a JSON response from a webservice using Guzzle 6 under Laravel 6.
The response i'm trying to decode is here:
https://json.volotea.com/dist/stations/stations.json
In my php code i try in this way:
$client = new Client();
$response = $client->request('GET','https://json.volotea.com/dist/stations/stations.json');
To decode the response i tried several methods like:
$response->getBody(); // that returns a stream
$response->getBody()->getContents(); that returns an apparently correct answer but if I run the json_decode it returns a null as a result
I'm going crazy!
How can i decode the response from this service?
many thanks
it seems with method getContent you already manipulated Json structure
try json_decode on $response.
Related
I have a static URL for a GET request, which is like - api.airtable.com/v0/<my-id>/VID?api_key=<api-key>. When I run this URL in chrome, I see a JSON response. But I want to fetch the response in PHP and work with it. How do I just run this simple api-request and get the JSON response in PHP?
I am a beginner to PHP, so this question might sound very basic to you :) Your help/advice is very appreciated - Thanks!
You can use the json_decode() function to convert your JSON into an array.
See documentation here: https://www.php.net/manual/en/function.json-decode.php
An example would be:
<?php
$json_data = file_get_contents("https://your-url-here");
$response_data = json_decode($json_data, true);
?>
Now your $response_data variable is an array, with which you can work normally.
Don't hesitate to ask if you need more help. :)
If JSON response is in array then you can use escapeJsonString() and json_decode() functions as below.
$response = escapeJsonString($response);
$response = json_decode($response,true);
print_r($response);
I am working on android app with api developed in PHP. I am trying to send an object to api, so I am serializing it with Gson library, and trying to deserialize in PHP code using Unserialize() function. But, its giving error as (!)Notice: unserialize(): Error at offset 0 of 468 bytes in C:\wamp64\www\digiclass\admin\api\upload_pending_results.php on line 5 and finally giving json response as {"error":false,"data":false} Here is my PHP code:
<?php
//an array to display response
$response = array();
$serObject = $_POST['serObject'];
$response['error'] = false;
$response['data'] = unserialize($serObject);
echo json_encode($response);
?>
currently, I am not performing any operation on data, rather only want to see if the data is deserializing correctly. I have tried to see the serialized object, it is converted as below.
%7B%22accessToken%22%3A%22b2c1f3d5e8218cfa81d23c4b9b7d6cbc20f82c67ca4b9d92be9bb7680031360a9d95bf1e3ecf42678f1c8b87a4eb5622%22%2C%22clas%22%3A%229%22%2C%22dbname%22%3A%22a_new%22%2C%22records%22%3A%5B%7B%22clas%22%3A%229%22%2C%22id%22%3A1%2C%22marks%22%3A%222%2F8%22%2C%22rollno%22%3A%2212%22%2C%22subject%22%3A%22Maths%22%2C%22temp%22%3A1%7D%2C%7B%22clas%22%3A%229%22%2C%22id%22%3A1%2C%22marks%22%3A%227%2F8%22%2C%22rollno%22%3A%2212%22%2C%22subject%22%3A%22Maths%22%2C%22temp%22%3A2%7D%2C%7B%22clas%22%3A%229%22%2C%22id%22%3A1%2C%22marks%22%3A%223%2F8%22%2C%22rollno%22%3A%2212%22%2C%22subject%22%3A%22Maths%22%2C%22temp%22%3A3%7D%2C%7B%22clas%22%3A%229%22%2C%22id%22%3A1%2C%22marks%22%3A%228%2F8%22%2C%22rollno%22%3A%2212%22%2C%22subject%22%3A%22Maths%22%2C%22temp%22%3A4%7D%5D%2C%22rollno%22%3A%2212%22%7D
I am not sure if the conversion is going right or what the error might be. Can anyone help?
The value you are getting is a URL encode JSON string, so first decode it...
$serObject = urldecode($serObject);
and then decode it as JSON...
$response['data'] = json_decode($serObject, true);
One of my partners has an API service which should send an HTTP POST request whenever a new file is published. This requires me to have an api file which will get the POST this way:
http://myip:port/api/ReciveFile
and requesting that the JSON format request should be:
{
"FILE ":"filename.zip",
"FILE_ID":"123",
"FILE_DESC":"PRUPOUS_FILE",
"EXTRAVAR":"",
"EXTRAVAR2":"",
"USERID":" xxxxxxxxxxxx",
"PASSWORD":"yyyyyyyyyyy"
}
Meanwhile it should issue a response, in JSON format if it got the file or not
{"RESULT_CODE":0,"RESULT_DESCR":""}
{"RESULT_CODE":1001,"RESULT_DESCR":"Bad request"}
And after, when I am finished elaborating the file, I should send back the modified file same way.
The question is, now basically from what I understand he will send me the variables witch I have to read, download the file, and send a response back.
I am not really sure how to do this any sample code would be welcomed!
I'm not sure exactly what the question is, but if you mean creating a success response in JSON for if an action occurred while adding data to it which is what can be understood from the question, just create an array with the values you wish to send back to the provider and do json_encode on the array which should create json and just print it back as a response.
About receiving the information; all you have to do is use the integrated curl functions or use a wrapper (Guzzle, etc) to output the raw JSON or json_decode data into a variable and do whatever you please with it.
From what I read in the question, you wish to modify the contents of it. This can be achieved by just decoding the json and changing the variables in the array, then printing the modified JSON back as a response.
Example (this uses GuzzleHTTP as an example):
$res = $client->request('GET', 'url');
$json = $res->getBody();
$array = json_decode($json, 1); // decode the json
// Start modifying the values or adding
$array['value_to_modify'] = $whatever;
$filename = $array['filename']; // get filename
// make a new request
$res = $client->request('GET', 'url/'.$filename);
// get the body of specified filename
$body = $res->getBody();
// Return the array.
echo json_encode($body);
References:
http://docs.guzzlephp.org/en/latest/
I use Unirest for PHP to do some HTTP Requests. Everything works fine, until I want to pass a fairly complex JSON to my Node.js router.
First I do a GET request that returns a JSON Object, then I extend this JSON object (needs to be done, I know it sucks) and want to feed it back into my other http POST request... and here is where the trouble starts:
I echoed the JSON that is returned and copied the output into postman -> works fine. If I want use the JSON directly in PHP in next request:
$teamsMemberOf is the variable which is containing the GET response.
$headers = array("Accept" => "application/json");
$newBody = '{"team":'.$teamsMemberOf->raw_body.'}';
$relevantBoxesAmount = Unirest\Request::post("http://localhost:3001/my/route/".$result['_id']."/get-something-from-server", $headers, $newBody);
and it doesn't work.
Error is 500 and 'Cannot read property '0' of undefined' which is certainly related to something in the JSON object.
Does someone have an idea how to fix it?
I'm still wondering how to get json object from REST Api request from this link https://developers.google.com/blogger/docs/3.0/using.
It says GET https://www.googleapis.com/blogger/v3/blogs/2399953?key=YOUR-API-KEY request will give informations about particular blog.
I'm also PHP noobs. Here's my code.
<?php
$blogID = 'xxx';
$apikey = 'yyy';
$requestURL = "https://www.googleapis.com/blogger/v3/blogs/{$blogID}?key={$apikey}";
$json = file_get_contents($requestURL);
print_r($json);
?>
I have no idea about this, so please help me to find where the problem is. Thanks in advance
Try this:
$json = json_decode(file_get_contents($requestURL));
You are fetching a JSON string and trying to print_r it, which only works on arrays.
json_decode, as its name implies, decodes the JSON string and transforms it into an array / object.