This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 6 years ago.
I have a page (insert.php) from which I am trying to return a response that contains $my_variable as a json array back to another page (form.php).
I can see this array in the response section of my firebug console but how can I get at it on form.php and use it with php?
insert.php is sending this:
// sending output
header('Content-Type: text/json');
echo json_encode(array("my_var" => "$my_variable"));
}
just use json_decode function
$arr = json_decode($_REQUEST['my_var'],true);
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I use Binance API.
The PHP function gets file contents of url:
https://api.binance.com/api/v1/klines?symbol=BTCUSDT&interval=4h&limit=1
I receive such a response:
[[1528545600000,"7635.01000000","7650.00000000","7566.00000000","7621.15000000","3246.04484200",1528559999999,"24674635.12120312",57351,"1932.28202000","14685353.70584041","0"]]
How to get the second value? (7635.01000000)
If you want to get the second value use this:
$receivedData = '[[1528545600000,"7635.01000000","7650.00000000","7566.00000000","7621.15000000","3246.04484200",1528559999999,"24674635.12120312",57351,"1932.28202000","14685353.70584041","0"]]';
$jsonDecodeData = json_decode($receivedData);
$secondValue = jsonDecodeData[0][1];
echo $secondValue; //7635.01000000
This question already has answers here:
Why does var_dump show filename and line number?
(2 answers)
Closed 5 years ago.
so here in the below code im calling nytimes api
<?php
function rpnyt_article_get_result( $rpnyt_search , $rpnyt_key ){
$rpnyt_url = 'https://api.nytimes.com/svc/search/v2/articlesearch.json?q='.$rpnyt_search.'&api-key='.$rpnyt_key ;
$json_feed = wp_remote_get($rpnyt_url);
var_dump($json_feed[ 'body']);
}
?>
im getting response back as expected but that includes file url from where im calling this function like /home/ubuntu/XXXXXXXXX/xxxxxxxxxxxx/plugins/XXxxx/includes/rpnyt-news-content.php:8:(see image)
Try var_export() instead of var_dump().
http://php.net/manual/en/function.var-export.php
Consider using
echo(json_encode($your_thing));
This question already has answers here:
Returning JSON from a PHP Script
(20 answers)
Closed 5 years ago.
I'm doing some tests in PHP to make an api and use it from nodejs,
i did something like :
<?php
function test () {
$var = 'School';
return json_encode($var);
}
echo test();
?>
it works great,
so my question is : do I need to display my response (with echo) ?
I get my results in nodejs. if I don't display my data with echo I receive an empty result.
Thanks for you help.
Yes. You may also want to send headers what sort of data you're returning (for example, application/json Content-Type) using header function (before echoing anything).
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I have a JSON output that looks like this:
{"data":[{"QID":"Q1234","MgrQID":"5678","NTID":"blah"}]}
I am trying to access just the data within the square brackets, inside the data array?
I have tried $jsonVar['data'] and $jsonVar->data and neither of them are working.
Is there a way I can just get access to [{"QID":"Q1234","MgrQID":"5678","NTID":"blah"}]?
Try this example:
<?php
$data = '{"data":[{"QID":"Q1234","MgrQID":"5678","NTID":"blah"}]}';
$test = json_decode($data, true);
echo json_encode($test['data']);
?>
Output:
[{"QID":"Q1234","MgrQID":"5678","NTID":"blah"}]
This question already has answers here:
json decode in php
(5 answers)
Closed 8 years ago.
How to spit this set of data using php? this data generated from some php to my mysql database.
[{"id":"1","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Company","value":"Destination Queenstown"},{"id":"2","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Your Name","value":"Ella Zhang"}]
now i just need to get id=2 and its value in the output.
It is a JSON data.Try with -
$array = '[{"id":"1","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Company","value":"Destination Queenstown"},{"id":"2","is_email":"false","add_to_day_hour_info":"false","add_to_day_hour_body":"false","translation":"Your Name","value":"Ella Zhang"}]';
$array = json_decode($array);
echo $array[1]->id;