php picasa api json decode problem - php

Here is my code to json decode picasa api results, but it always Warning: Invalid argument supplied for foreach(), Where is the problem? Thanks.
<?php
header('Content-type:text/html; charset=utf-8');
$url="http://picasaweb.google.com/data/feed/base/all?alt=json&kind=photo&access=public&filter=1&q=usa&imgor=landscape&max-results=50&hl=en";
$json = file_get_contents($url);
$data = json_decode($body, true);
foreach ($data['feed']['entry'] as $result){
echo html_entity_decode($result->content->src, ENT_QUOTES, 'UTF-8');
echo html_entity_decode($result['updated']['$t'], ENT_QUOTES, 'UTF-8');
}
?>
json tree here

You are using a wrong variable:
$json = file_get_contents($url);
$data = json_decode($body, true);
It should be $json instead of $body.
You used json_decode(, true) so it converts all objects to associative arrays. But here you use object-notation to access your data which might result in another error:
echo html_entity_decode($result->content->src, ENT_QUOTES, 'UTF-8');

Related

json_decode returns null and bool false

I have code:
<?php $json = '[{"title":" \\ud83c\\uddf7\\ud83c\\uddfa \\u0420\\u043e\\u0441\\u0441\\u0438\\u0439\\u0441\\u043a\\u043e\\u0435 \\u043a\\u0430\\u0437\\u0438\\u043d\\u043e #1
\\ud83c\\uddf7\\ud83c\\uddfa ","desc":"\\u0422\\u043e\\u043b\\u044c\\u043a\\u043e
\\u0437\\u0430\\u0440\\u0435\\u0433\\u0438\\u0441\\u0442\\u0440\\u0438\\u0440\\u0443\\u0439\\u0441\\u044f \\u0438 \\u0438\\u0433\\u0440\\u0430\\u0439
\\u043f\\u0435\\u0440\\u0432\\u044b\\u0439 \\u0440\\u0430\\u0437 \\u0437\\u0430 \\u043d\\u0430\\u0448
\\u0441\\u0447\\u0451\\u0442","icn":"https://1.mbvnclick1.com/ic?sid=2&data=0eikgji0Ck2EKXJkLTJfLie%2FKy%2FvWYZiVPrhxIOQsl6VkyioGiy%2B4DYdpqaaMXlM5dVPkQoRzngoPAlvQ3w1pREOxlMjuR7DQHq6Yz0oA7ZXT9CV1ut2ICfrquV9FoQ%2BjltIeJAcUnB%2BTMvTjn%2BGs1lvh5bOIUUXYa0tIJCe%2BJe2LX38OpOLAJ%2B7U1h12rvXozelMT5SGd67wzUnFI7er3gJycSu7WAH72sUTT%2FZ%2F3nJQOZBOMHY8WyX8jqel5Mo8BMNLzIXHyjpA%2BiZlgYsEg%3D%3D","img":"https://1.mbvnclick1.com/im?sid=2&data=ZQqgvmU6z8ZR4RPBdAhPWcdkbt5b%2BWp435ln18YHYo1SXskUGSiZhGwhvcXnWECjuteCzRQRWIhfYTUDd4wLcUq7jKaYn55gJUbQZr3UM6SAx2dKKXUVQVmstTsIdXma7gZ57%2B8L58uusM7pf8HpgSTreH8rjJIX%2BQEruq544CQQF%2FTNxTpCAesrBgQpkUOL76hSB%2F0Eaw3yYO0mDUDR6zKLXkDo6cxruIRrER05RSFJVtlFr3ihmDZHJQZnl%2FO6","url":"https://1.mbvnclick1.com/clpsh?sid=2&d=1&data=h3OP98W8RXI52WXh0xUpzzPCqkn%2Boc1q7OZh2tb7pLxLU4il0MNlbTTRR%2FQJ3Ryj98kKbM2eOgq%2FVtMBpmy4huEGwavyp41rQdZTT%2Fjdsu0QcYNMwUiNBH4mifSNaIzMTDYTeB9hZ8BPwGw%2F171wk2af2qmrmLi7e13XtfK%2BFpZltozDNAqS%2BDJkvH3SVKJHo8TkGjb2FQonQoXeVXqfp6jp2MYLqp%2FOFf6dOcERVM%2Ff%2FYBgEZ2E%2FpzuMZSywxPt49sveDcfOE%2F9LOjBu6%2BU1XymVQdknq%2B0MzuJAd6Eq8%2FH4q%2F%2B7dlgvivqQm30C%2FvhG%2FfGSYQPEY%2BHdzAJZ%2FStRjZmMtGhsqHbMkGENTil4bzlo8VvMW6H2yLPpVVw8Eqw86jXlndl7qPusmT4W4VUVQzMEnKgDbiJFPGy45vE%2B3QOCqafNoCq90X7U%2FLlvr9Gxdox8qAUyhAMbqJU5p0GYlMk6iJDD3GaG%2FqAZN5hzM0%3D","price":"0.0055"}]';
$json = json_decode($json, true); var_dump($json);
echo '<br>';
echo json_last_error();
?>
It returns null and if I check JSON for errors it returns bool(false). What is wrong? Online decoders are decoding json normally. JSON is advertisment I'm using for test.
I've been searching for issue for hours. I've deleted urls from json, checking if another jsons are working on site (they work). IDK what to do
$json = preg_replace('/[[:cntrl:]]/', '', $json); $array = json_decode($json, true);
I should preg_replace before decoding JSON

Accessing the results from CURL request

Im making a CURL request in PHP, and trying to echo the ID result.
$responseData = request();
echo $responseData["id"];
The results are
{
"result":{
"code":"000.200.100",
"description":"successfully created checkout"
},
"buildNumber":"3b5605d6df9e6068ff1e9b178947fc41e641456e#2019-11-26 03:42:27 +0000",
"timestamp":"2019-11-27 13:19:29+0000",
"ndc":"B244D47DBFAD13EDEF126C980A711C8D.uat01-vm-tx02",
"id":"B244D47DBFAD13EDEF126C980A711C8D.uat01-vm-tx02"
}
But when trying to get the ID i am getting the error
Warning: Illegal string offset 'id' in \public\payment\index.php on line 25
JSON can easily be parsed in PHP using json_decode(). There is an optional flag to decode the JSON into an object or an array.
Decode as an object:
$json = request();
$data = json_decode($json);
echo $data->id;
Decode as an array:
$json = request();
$data = json_decode($json, TRUE);
echo $data['id'];
if you can decode you json using json_decode and then access the id variable like you are doing will be good for you.
$data = request();
$responseData = json_decode($data);
echo $responseData["id"];

How to Json In PHP 'iyas'

http://simsimi.com/getRealtimeReq?uuid=VbQqwsQ3qiCw1T04VrrFQzBBlkfZibg3YFkbWz6VquZ&lc=id&ft=1&reqText=Eh+jelek&status=W
How to get value from respSentence
Im using php lang ^_^
" {"status":200,"respSentence" 11 \:"respSentence"}"` is not valid json. Use some online sites to validate your json and try like this,
$json = '{"status":200,"respSentence" :"iyas"}';
$array = json_decode($json, true);
echo $array ['respSentence'];
You can try using file_get_contents and json_decode PHP functions
$url = "http://simsimi.com/........";
$data = file_get_contents($url);
$json = json_decode($data, true);
echo $json ['respSentence'];
hope it helps

Php json decode array

i have a json string. But i can not access values.
$json_string : '{"05526":[{"name":"rapertuar","surname":"xyz","notes":[{"mat1":"59","eng2":"60"},{"mat2":"59","eng2":"60"}]}]}';
$content = file_get_contents($json_string);
$json_a = json_decode($content, true);
echo $json_a['05526']['0']['name'];
echo $json_a['05526']['0']['name']['notes']['0']['mat1'];
How can i fix this code? Thank you
$json_string : '{"05526":[{"name":"rapertuar","surname":"xyz","notes":[{"mat1":"59","eng2":"60"},{"mat2":"59","eng2":"60"}]}]}';
// you don't need this line
//$content = file_get_contents($json_string);
$json_a = json_decode($json_string, true);
echo $json_a['05526']['0']['name'];
echo $json_a['05526']['0']['name']['notes']['0']['mat1'];
There's no need to use file_get_contents if you're storing the JSON in a string and then decoding it. Follow the below approach:
$json_string = '{"05526":[{"name":"rapertuar","surname":"xyz","notes":[{"mat1":"59","eng2":"60"},{"mat2":"59","eng2":"60"}]}]}';
$json_a = json_decode($json_string, true);
echo $json_a['05526']['0']['name']; // rapertuar
echo $json_a['05526']['0']['notes']['0']['mat1']; // 59

How to output json data in array using curl

I have code here that will get json data using curl. Now I want to echo all data. This will show no output.
<?php
$json = file_get_contents('myurl');
$data = json_decode($json);
print_r($data);
?>
This is json coming from my url:
({"Response_Code":"0000","ResultMobilePrefix":["0917","0905","0906","0915","0916","0926","0927","0937","0935","0817","0936","0922","0923",
"0932","0933","0934","0942","0943","0907","0908","0909","0910","0912","0918","0919","0920","
0921","0928","0929","0930","0938","0939","0948","0949","0925","0989","0999","0947","0998","
0946","0975","0977"]});
All you need is
echo json_encode($json);
Use true to convert it into array;
$data = json_decode($json,true);
echo '<pre>';
print_r($data);
for displaying json in format use json_encode() and for proper format use JSON_PRETTY_PRINT
header('Content-type: application/json');
echo json_encode($json,JSON_PRETTY_PRINT);
you need to get rid of the bracket and semicolon on the JSON file first before you can use the json_decode();
<?php
$json = file_get_contents('myurl');
//remove the brackets
$json = str_replace("(", "", $json);
$json = str_replace(")", "", $json);
//remove the semicolon
$json = str_replace(";", "", $json);
$data = json_decode($json);
print_r($data);
?>
this is a bit ugly, but hope you get the idea, you need to remove that character first
I just want to add to what Manish has explained above. This is what PHP Docs say about the second parameter:
When TRUE, returned objects will be converted into associative arrays.
else you will simply get a stdclass object
The problem is the URL does not return a valid Json
you could simply try
var_dump($data);
this will return null because it's not a valid json see json_decode
this will work
$json = file_get_contents('myurl');
$json = preg_replace('/[ ]{2,}|[\t\n\r\(\)\;]/', '', trim($json));
$data = json_decode($json);
print_r($data);

Categories