tags:

views:

257

answers:

3

I have a json feed from zoho : here, you can acces the same file unencrypted here I like to be able to display and parse the data from that feed int html

I have ask a similar question yesterday, but the solution was a javascript, and having java disable client side can lead to nothing to display... so i will go with php. I can parse a var but a feed ?....

Question #2. Is it possible to capture a json feed, and save it as file (for backup purpose), so i will acces that file if the site go down (small possibilites)

+5  A: 

Hi,

You first have to get the JSON data from the remote server ; not sure how you can do that, considering there seems to be an authentication mecanism in place, but maybe file_get_contents or curl could help.

Once you have that JSON string in a PHP variable, it's just a matter of calling json_decode on it, to get your data.


For instance :

$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);

Will get you an output like this one :

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 kind of API you need to use to access the crypted one (never used "zoho")

Pascal MARTIN
genius !... will code it base on your infos !, thanks 100k
marc-andre menard
any way to display it a nicer way (to be able to reed it )
marc-andre menard
To display it to read it... Heu... for debugging purposes ? var_dump, with the http://xdebug.org/ extension installed, is perfect ;-) (and that extension is really nice, on a development machine -- don't install it on a production server, though : not good for performances)
Pascal MARTIN
maybe a method to acces the variable ? i will show it my way !...i have try something like : print_r($result.Liste_des_produits1.prod_ingredient);
marc-andre menard
Je peut peut etre te poser quelques autres questions par email perso ([email protected]) si tu veut ?
marc-andre menard
print_r($data->Liste_des_produits1[0]->prod_ingredient;
andho
@marc-andre : I would rather prefer not, please
Pascal MARTIN
+1  A: 

The 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 off or you need authentication headers (or other special headers set), use the curl libraries in place of file_get_contents.

Alan Storm
perfect solution. just as the one above... 4 minutes later... but thanks !
marc-andre menard
A: 

I like to be able to print/access some variable but cannot seem to have the trick to access the array the wright way

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
marc-andre menard