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);
Related
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.
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 am sending json object from android as such:
//Create JSONObject here
JSONObject json = new JSONObject();
json.put("key", String.valueOf(args[0]));
String postData=json.toString();
// Send POST output.
printout = new DataOutputStream(urlConn.getOutputStream ());
printout.writeUTF(URLEncoder.encode(postData,"UTF-8"));
Log.i("NOTIFICATION", "Data Sent");
printout.flush ();
printout.close ();
When it is sent to the server it looks like the following code snippet. ???%7B%22key%22%3A%22value%22%7D
I should add the first ??? are in a diamond each. When I decode the whole json object I get null. In the php server I have
$somevar=json_decode(json, true);
which returns null. Can someone point me on how to retrieve the json value? Thanks so much:)
I'm trying to get some information out of my incomming messages from Google Cloud Message (GCM). Message looks like this:
{
"category":"com.myappplication",
"data": {
"my_message":"this data i need",
"my_action":"com.google.android.gcm.demo.app.ECHO_NOW"
},
"time_to_live"86400,
"message_id":"5",
"from":"ADJEKRJEKRJEKJREKRJLSDLKSJDLKJ23DSD22232320DSLKJ23"
}
I only can get the data out of the "from","message_id" and "time_to_live".
In my Php script I decode the incomming json message
$gcm_in = json_decode(str_replace(""", "\"", $stanza_in->childrens[0]->text));
$from = $gcm_in->from;
How to get the information of my_message?
Considering the json data that you specified is stored in variable $data.
$objData = json_decode($data);
echo $objData->data->my_message;
json_decode function converts data fomr json format to php object.
Though I am not sure why tried to replace " in your code and originally in which variable you are receiving data.
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.