So I have this json file (json.json) stored on a server, I would like to overwrite it with new json Data ('refresh_token' object to be exact) everytime I call on this php class:
$file = file_get_contents('/var/json.json');
$json = json_decode($file);
**$REFRESH_TOKEN** = $json->refresh_token;
$json = file_get_contents("https://xxxxxxxxxxx&refresh_token=**$REFRESH_TOKEN**");
$obj = json_decode($json, true);
$AccessToken = $obj->access_token;
$RefreshToken = $obj->refresh_token;
**file_put_contents('/var/json.json', json_encode($RefreshToken));**
The json file looks like this:
{"token_type":"bearer",
"expires_in":3600,
"scope":"xxxxxxx",
"access_token":"xxxx",
**"refresh_token":"xxxxx",**
"user_id":"xxxxxx"}
I repeatedly get the error: Trying to get property of non-object in whenever I call the class for a second time. I looked inside the json file after the first call and realized that it did not contain anything. This indicates that the file_put_contents('/var/json.json', json_encode($RefreshToken)) did not work.
Can anyone give me a hint as to what i'm doing wrong here?
Make sure you have permission to write on the file. Change file permission to 777.
From your code, it is not obvious what you are trying to do. I guess it is the following:
fetch json object from file
modify refresh token
write json object back to file
This would work as follows:
$REFRESHTOKEN = $_GET['REFRESH_TOKEN']; // Calculate refresh token
$json = json_decode(file_get_contents('/var/json.json')); // fetch json object from file
$json->refresh_token = $REFRESHTOKEN // modify refresh token
file_put_contents('/var/json.json', json_encode($json)); // write json object back to file
What you are doing is writing ONLY the refresh token into the file, not the whole object. This is why on the second attempt, the object is not there...
If you set the 2nd parameter of json_decode to true you get an associative array instead of an object.
Documentation for json_decode: http://php.net//manual/en/function.json-decode.php
Related
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);
I am trying to get the POST data that is being sent to a controller method within my CI project. However, I am not able to get this data which ever way I try to get it.
I have checked NGROK Inspect and Im able to see the data that I want as you can see in the screenshot below:
The problem is when I try to access this date from the controller method itself, I get nothing, not sure if Im accessing it wrong, but the following are what I have tried:
$Data = json_decode(file_get_contents('php://input'), true); //Method 1
$response = $this->input->post("Body"); // Method 2
$data = $this->input->post(); //Method 3
I get nothing whenever I try to use the methods above.
This is how the Controller method looks like, basically, it creates a json file and saves the response there:
public function resolve_payment(){
$data = $this->input->post();
$fp = fopen("testdir.json", 'w');
fwrite($fp, json_encode($data, JSON_UNESCAPED_SLASHES));
fclose($fp);
}
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'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.
I'm using the Requests for PHP library to POST some json data to another PHP script (I'm using Laravel's Response::json method to generate the json output:
public function postIndex()
{
$input = Input::get();
$data = Response::json($input);
$url = 'http://mydomain.com/emails/events';
$response = Requests::post($url, array('Content-Type' => 'application/json'), $data);
return $response->status_code;
}
I need the script on the receiving end (http://mydomain.com/emails/events) to decode and process the json, but I'm having a hard time accessing it. I setup a simple test script that emails me the contents of $_POST, but it comes up empty every time.
$post_data = print_r($_POST,true);
mail("my#email.com","post data",$post_data);
What am I doing wrong here?
PHP do not parse Json POST. So you need to get raw post data like this:
$data = file_get_contents("php://input");
Info about php php wrappers