2

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)

4 Answers 4

7

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")

Sign up to request clarification or add additional context in comments.

6 Comments

any way to display it a nicer way (to be able to reed it )
To display it to read it... Heu... for debugging purposes ? var_dump, with the 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)
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);
Je peut peut etre te poser quelques autres questions par email perso ([email protected]) si tu veut ?
print_r($data->Liste_des_produits1[0]->prod_ingredient;
|
2

json_decode will convert the json string to object form by default, in order to use it as associative array you have to specify 'true' as second parameter, example below. json_decode($json,true)

reference : php Json_decode reference

Comments

1

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.

1 Comment

perfect solution. just as the one above... 4 minutes later... but thanks !
0

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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.