This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
i have a problem when i try to get the object value there's nothing in it. but when i just echo the object , there's no null i dont know how to get that value
if(isset($_POST['submit'])) {
$secret = "6Le3nvgUAAAAAMU0DRNLtvtaIq3h6X9ybEnO_txv";
$captcha = $_POST['g-recaptcha-response'];
$url = 'https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$captcha;
$response = file_get_contents($url);
echo $response;
}
i got response
but when i try to $response-> success . it return null ..
The response you 've got is written in JavaScript Object Notation (JSON). For this purpose PHP got it 's own JSON decode and encode functions.
$decodedResponse = json_decode($response);
var_dump($decodedResponse->success);
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 1 year ago.
{"ServiceClass":{"MessageId":"1633236883931745","Status":"0","StatusText":"success","ErrorCode":"0","ErrorText":{},"SMSCount":"1","CurrentCredit":"49.24"}}
use first Json decode
$json_response ='{"ServiceClass":{"MessageId":"1633236883931745","Status":"0","StatusText":"success","ErrorCode":"0","ErrorText":{},"SMSCount":"1","CurrentCredit":"49.24"}}';
$array = json_decode($json_response, true);
function extractProperty($arr, $propName){
return $arr['ServiceClass'][$propName];
}
echo 'MessageId : '.extractProperty($array, 'MessageId'). "<br>";
echo 'CurrentCredit : '.extractProperty($array, 'CurrentCredit');
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
I have a response from my code which looks like this
Controller
$results = $response
echo $result->item;
exit;
Response
{"item":"Samsung A","location":"Hamburg Avenue 5920"}
I want to get only item from the response..How do i achieve this ? The response is json encoded
You need to convert json to object, and after that you can get the element:
$respObject = json_decode($response);
echo $respObject->item;
exit;
json_decode function will helps.
$json_data = '{"item":"Samsung A","location":"Hamburg Avenue 5920"}';
$result = json_decode($json_data);
echo $result->item;
Use json_decode the result will converted into array
$results = json_decode($response);
print_r( $results);
exit;
This question already has answers here:
Cannot use object of type stdClass as array?
(17 answers)
Closed 8 years ago.
HeIIo, I'm sending a json array via PUT method. The problem is, that on PHP side, I don't know how to handle it and drop it into pieces, such as
$data['id'] and $data['name']
What I tried was
$input = file_get_contents('php://input');
$data = json_decode($input);
echo print_r($data);
$id = $data['id'];
echo $id;
It echoes a pretty normal looking array, but the 4th line gives me the
<b>Fatal error</b>: Cannot use object of type stdClass as array in <b>/home/alexa449/public_html/biom/websitec/php/api.php</b> on line
Can someone help to handle this. Thank you.
You can try:
$content = stream_get_contents(fopen('php://input', 'r'));
parse_str($content, $parameters);
var_dump($parameters);
This question already has answers here:
How to convert JSON string to array
(17 answers)
Closed 8 years ago.
When I try to get data from an API using
file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDINR%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");
I got the result as a string. Is there any way to convert it back to array?
The string which I got is
string(202) "{"query":{"count":1,"created":"2014-03-11T13:00:31Z","lang":"en-US","results":{"rate":{"id":"USDINR","Name":"USD to INR","Rate":"60.99","Date":"3/11/2014","Time":"9:00am","Ask":"61.00","Bid":"60.98"}}}}"{"error":"","msg":""}
please help me....
In your request, you ask that the returned format be JSON-encoded (via the format=json parameter), so you can just decode the response into an array using json_decode:
$response = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDINR%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");
$response = json_decode($response, true);
// Parse the array as you would any other
You have a JSON answer here so jo need to dekode that.
<?php
$s = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDINR%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");$data =
$data = json_decode($s,true);
?>
and the $data variable will be an array.
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 11 months ago.
i got this json encoded data being sent to me.
can u tell me how to get each of the individual elements ?
Something like:
$ticket
$customer
$user
{"ticket":{"id":"10909446","number":"152"},"customer":{"id":"3909381","fname":"","lname":"","email":"me#site.com","emails":["me#site.com"]},"user":{"fname":"Test","lname":"Me","id":17396,"role":"admin"}}
this is a basic view on how my code runs.
$ret = array('html' => '');
$data = json_decode($data , true);
$ret['html'] = '<ul><li>'.$data->ticket->number.'</li></ul>';
echo json_encode($ret);
exit;
it only prints the circle from the li tags.
To clarify #Cthulhu's answer :
$test = '{"ticket":{"id":"10909446","number":"152"},"customer":{"id":"3909381","fname":"","lname":"","email":"me#site.com","emails":["me#site.com"]},"user":{"fname":"Test","lname":"Me","id":17396,"role":"admin"}}';
$data = json_decode($test);
echo $data->ticket->id;
outputs
10909446
json_decode make the JSON into a stdClass object, and then you can access the values as that.
$data = json_decode($test);
$ret = array();
$ret['html']='<ul><li>'.$data->ticket->number.'</li></ul>';
return
json_encode($ret);
will return
{"html":"<ul><li>152<\/li><\/ul>"}
json_decode is the answer for you.