I've a JSON string, I want to get it in store of Extjs by a PHP url. How to get this string as a JSON from a php file. Something like this:
Extjs file:
PHP file:
The result is blank.
What I do now for this work?
An example of what I believe you mean to do:
$array = array();
$test = '{ "firstName":"John" , "lastName":"Doe" }';
$array[] = json_decode($test);
$test = '{ "firstName":"Jane" , "lastName":"Doe" }';
$array[] = json_decode($test);
$test = '{ "firstName":"Random" , "lastName":"Guy" }';
$array[] = json_decode($test);
print json_encode($array);
exit;
Will give you:
[{"firstName":"John","lastName":"Doe"},{"firstName":"Jane","lastName":"Doe"},{"firstName":"Random","lastName":"Guy"}]
As for the Ajax, maybe this will help - How to get "data" from JQuery Ajax requests, or maybe try the Jquery Ajax documentation.
Related
I have a json i am fetching remotely in php,
the Json returned is as follow:
{
"base_currency_code":"USD",
"base_currency_name":"United States dollar",
"amount":"1.0000",
"updated_date":"2022-01-30",
"rates":{
"KWD":{
"currency_name":"Kuwaiti dinar",
"rate":"0.3030",
"rate_for_amount":"0.3030"
}
},
"status":"success"
}
the i try to get the specific value from key : rate :
$url='https://...';
$json = file_get_contents($url);
$obj = json_decode($json,true);
echo $obj['rates']['KWD']['rate'];
but I have no output, when I would like to have 0.3030. Why is this?
test.json
{
"base_currency_code":"USD",
"base_currency_name":"United States dollar",
"amount":"1.0000",
"updated_date":"2022-01-30",
"rates":{
"KWD":{
"currency_name":"Kuwaiti dinar",
"rate":"0.3030",
"rate_for_amount":"0.3030"
}
},
"status":"success"
}
test.php
<?php
$url='test.json';
$json = file_get_contents($url);
$obj = json_decode($json,true);
echo $obj['rates']['KWD']['rate'];
Result 0.3030
If it is not working, you cannot access the site remotely.
try it and make sure it works
<?php
$url='https......';
$json = file_get_contents($url);
echo $json;
Hi Guys I have a JSON data I need to convert this data Treeview a json data url: http://torrent2dl.ml/json.php
recovered state = http://torrent2dl.ml/json.php?tree
I tried to do http://torrent2dl.ml/hedef.php
how to convert this data a php function or code ?
json_decode($jsonObject, true);
Use json_decode() :
<?php
$url = 'http://torrent2dl.ml/json.php';
$JSON = file_get_contents($url);
// echo the JSON (you can echo this to JavaScript to use it there)
echo $JSON;
// You can decode it to process it in PHP
$data = json_decode($JSON);
var_dump($data);
?>
Source : https://stackoverflow.com/a/8344667/4652564
I'm trying to record data that is being posted to my server to a text file. An example of the data that is being sent to my server is located here:
http://dev.datasift.com/docs/push/sample-output-file-based-connectors
It says on that page:
"For all file-based connectors, the payload is a JSON object containing metadata plus an array containing the JSON objects from your stream."
I have the following PHP at the location I have datasift sending data to:
<?php
$myFile = "testFile.txt";
$phpObj = json_decode($_POST['json']);
file_put_contents($myFile,$phpObj);
echo '{ "success": true }';
?>
I know data is being sent, but nothing is being recorded in my text file. It's just blank every time. I have no idea where to go from here unfortunately. Any ideas?
I think you want to get the raw content of the POST, This works for me with both POST and PUT:
$phpObj = json_decode(file_get_contents("php://input"));
$_POST contains the array from x-www-form-urlencoded content, not json.
Hope that points you in the right direction :)
Edit: #user4035 is right... your also trying to save a php object to a file... This is what i would do:
<?php
$jsonString = file_get_contents("php://input");
$myFile = "testFile.txt";
file_put_contents($myFile,$jsonString);
echo '{ "success": true }';
?>
You are trying to save an object, using file_put_contents. While data parameter this function "Can be either a string, an array or a stream resource"
http://php.net/manual/en/function.file-put-contents.php
Look at this example:
<?php
$json = '
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
';
$phpObj = json_decode($json);
var_dump($phpObj);
$myFile = "testFile.txt";
file_put_contents($myFile, $phpObj);
?>
It parses json correctly, but doesn't save anything as well, because php doesn't know, how to serialize $phpObj object.
You need to save raw JSON string:
<?php
$myFile = "testFile.txt";
file_put_contents($myFile,$_POST['json']);
echo '{ "success": true }';
?>
Then you can read the file and parse it if necessary.
I try this method and it works success. So, I share this here.
Source PHP save POST data to file
web_folder/index.php
<?php
$responseBody = file_get_contents('php://input');
$json = json_decode($responseBody);
//return data
echo json_encode($json);
//Save in json file
if($json){
$fp = fopen('results_'.time().'.json', 'w');
fwrite($fp, json_encode($json));
fclose($fp);
}
?>
and run the below code in the terminal for testing, it will create a JSON file with the POST request in the same folder. (Here I used localhost) or you can test using Postman.
curl -i -X PUT -d '{"name":"codehref"}' http://localhost:8888/web_folder/index.php
To bypass the problem I used JSON.stringify.
'formSave' : function(project){
var s = {
"name": project.name,
"data": JSON.stringify(project.data)
};
$.post("sdcform/formSave/" + project.name, s);
}
The 'project' object contains the keys 'name' and 'data' and I only wanted to stringify the data part of it.
This way on the server I can do
$data = isset($_POST['data']) ? json_decode($_POST['data']): new \stdClass();
file_put_contents($filename, json_encode($data, JSON_PRETTY_PRINT));
I could store it directly to the file and save both conversion but I wanted to prettyfy it!
Note: Yes I know! Do not access $_POST directly.
My Code
var json = xmlhttp.responseText; //ajax response from my php file
obj = JSON.parse(json);
alert(obj.result);
And in my php code
$result = 'Hello';
echo '{
"result":"$result",
"count":3
}';
The problem is: when I alert obj.result, it shows "$result", instead of showing Hello.
How can I solve this?
The basic problem with your example is that $result is wrapped in single-quotes. So the first solution is to unwrap it, eg:
$result = 'Hello';
echo '{
"result":"'.$result.'",
"count":3
}';
But this is still not "good enough", as it is always possible that $result could contain a " character itself, resulting in, for example, {"result":""","count":3}, which is still invalid json. The solution is to escape the $result before it is inserted into the json.
This is actually very straightforward, using the json_encode() function:
$result = 'Hello';
echo '{
"result":'.json_encode($result).',
"count":3
}';
or, even better, we can have PHP do the entirety of the json encoding itself, by passing in a whole array instead of just $result:
$result = 'Hello';
echo json_encode(array(
'result' => $result,
'count' => 3
));
You should use json_encode to encode the data properly:
$data = array(
"result" => $result,
"count" => 3
);
echo json_encode($data);
You're using single quotes in your echo, therefore no string interpolation is happening
use json_encode()
$arr = array(
"result" => $result,
"count" => 3
);
echo json_encode($arr);
As a bonus, json_encode will properly encode your response!
Try:
$result = 'Hello';
echo '{
"result":"'.$result.'",
"count":3
}';
$result = 'Hello';
$json_array=array(
"result"=>$result,
"count"=>3
)
echo json_encode($json_array);
That's all.
<?php
$handle = fopen("https://graph.facebook.com/search?q=sinanoezcan#hotmail.com&type=user&access_token=2227472222|2.mLWDqcUsekDYK_FQQXYnHw__.3600.1279803900-100001310000000|YxS1eGhjx2rpNYzzzzzzzLrfb5hMc.", "rb");
$json = stream_get_contents($handle);
fclose($handle);
echo $json;
$obj = json_decode($json);
print $obj->{'id'};
?>
Here is the JSON: {"data":[{"name":"Sinan \u00d6zcan","id":"610914868"}]}
It echos the JSON but I was unable to print the id.
Also I tried:
<?php
$obj = json_decode($json);
$obj = $obj->{'data'};
print $obj->{'id'};
?>
Note that there is an array in the JSON.
{
"data": [ // <--
{
"name": "Sinan \u00d6zcan",
"id": "610914868"
}
] // <--
}
You could try $obj = $obj->{'data'}[0] to get the first element in that array.
data is an array, so it should be:
print $obj[0]->{'id'};
It looks like the key "data" is an array of objects, so this should work:
$obj = json_decode($json);
echo $obj->data[0]->name;
Have you tried $obj->data or $obj->id?
Update: Others have noted that it should be $obj->data[0]->id and so on.
PS You may not want to include your private Facebook access tokens on a public website like SO...
It's a bit more complicated than that, when you get an associative array out of it:
$json = json_decode('{"data":[{"name":"Sinan \u00d6zcan","id":"610914868"}]}', true);
Then you may echo the id with:
var_dump($json['data'][0]['id']);
Without assoc, it has to be:
var_dump($json->data[0]->id);