How to parse json (feed) from PHP

I have a json feed from zoho: here you can get the same file as unencrypted here I like to display and parse data from this int html feed

I asked a similar question yesterday but the solution was javascript and with java disable the client side can't show anything ... so I'll go with php. I can parse var, but feed? ....

Question number 2. Is it possible to grab a json feed and save it as a file (for backup purposes), so I will use that file if the site works (small possibilities)

+2


source to share


4 answers


First you need to get JSON data from a remote server; not sure how you can do this, given that there seems to be an in-place authentication mechanism, but maybe file_get_contents

or curl

might help.

Once you have this JSON string in a PHP variable, you just need to make a call json_decode

to get your data.


For example:

$json = file_get_contents('http://produits-lemieux.com/json.txt');

// you can save $json to a file, if needed :
//file_put_contents('file/path/my-file.txt', $json);

$data = json_decode($json);

var_dump($data);

      



You will get output like this:

object(stdClass)[1]
  public 'Liste_des_produits1' => 
    array
      0 => 
        object(stdClass)[2]
          public 'Added_Time' => string '28-Sep-2009 16:35:03' (length=20)
          public 'prod_ingredient' => string 'sgsdgds' (length=7)
          public 'prod_danger' => 
            array
              0 => string 'sans danger pour xyz' (length=20)
    ....

      


Note. I used the "not encrypted" version because I have no idea what API you need to use to access the encrypted (never used "zoho")

+7


source


json_decode converts the json string to the default object form, to use it as an associative array, you must specify "true" as the second parameter, example below. json_decode ($ JSON, true)



link: link to php Json_decode

+2


source


Simplified code to do what you want.

$sJson = file_get_contents('http://example.com/path/to/feed.json');
file_put_contents('/path/to/file', $sJson);
$oJson = json_decode($sJson);   
var_dump($oJson);

      

If URL Wrappers are disabled, or you need authentication headers (or other special headers), use curl libraries instead file_get_contents

.

+1


source


I like to be able to print / access some variable, but it can't seem like it has a trick to access an array.

based on the previous answer:

$json = file_get_contents('http://produits-lemieux.com/json.txt');
$data = json_decode($json);
//var_dump($data);
print_r($data['prod_ingredient']); //dont work !... error 
print_r($data['Liste_des_produits1'].prod_ingredient[0]); //dont work !... error 

      

0


source







All Articles