PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: data from json
data from json - phponwebsites.com
Showing posts with label data from json. Show all posts

8 Oct 2014

Parse / Retrieve data from json file using php

                       You know how to create JSON file using PHP. Then we are going know how to get data from json file.

                       That means,  The json file returns encoded values. You have to decode it to display at wherever you wants. You can get data string from JSON using php.

                       The JSON file returns value as an array format, object format or combined both. See my previous post How to create JSON file using PHP.
                       In previous post, It give output like this:

       [    
           {
              'tutorial':'PHP',
              'link':'http://www.phponwebsites.com/p/php.html'
           },
           {
              'tutorial':'Mysql',
              'link':'http://www.phponwebsites.com/p/mysql.html'
           },
           {
            'tutorial':'htaccess',
            'link':'http://www.phponwebsites.com/p/htaccess.html'
           }    
      ]

Get data from JSON file using curl in PHP


               Now, you retrieve data from JSON file using PHP. Consider the following PHP script for retrieve data from JSON file.

  <?php

    $ch = curl_init("http://www.phponwebsites.com/json.php"); // add your url which contains json file
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $content = curl_exec($ch);
    curl_close($ch);
    $json = json_decode($content, true);
    //print_R($json);
    $count=count($json);
    echo'<table><th>Tutorial</th><th>Link</th>';
    for($i=0;$i<$count;$i++)
    {
      echo'<tr><td>'.$json[$i]['tutorial'].'</td><td>'.$json[$i]['url'].'</td></tr>';
    }
    echo'</table>';

?>

      where,
                  json_decode() - takes JSON encoded values and converts it to PHP variable
If you want to know about cURL, then visit php.net

Get data from JSON file using file_get_contents in PHP



 <?php
    $content=file_get_contents("http://www.phponwebsites.com/json-api.php");  // add your url which contains json file
    $json = json_decode($content, true);
   // print_R($json);
    $count=count($json);
    echo'<table><th>Tutorial</th><th>Link</th>';
    for($i=0;$i<$count;$i++)
    {
      echo'<tr><td>'.$json[$i]['tutorial'].'</td><td>'.$json[$i]['url'].'</td></tr>';
    }
   echo'</table>';
  ?>

           
       where,
                   file_get_contents() - reads whole values in file into a string

              When you print_r() the results, you will get output like this:

Array (
         [0] => Array ( [PHP] => Differences The Ranch House
                               [link] => http://www.phponwebsites.com/p/php.html
                             )
         [1] => Array ( [Mysql] => Kids Bus Kiss
                               [link] => http://www.phponwebsites.com/p/mysql.html
                            )
         [2] => Array ( [htaccess] => Beehive Hideout
                                [link] => http://www.phponwebsites.com/p/htaccess.html
                           )
          )

Finally, you will get output like this:

TutorialLink
PHPhttp://www.phponwebsites.com/p/php.html
Mysqlhttp://www.phponwebsites.com/p/mysql.html
htaccesshttp://www.phponwebsites.com/p/htaccess.html


Related Post:
Create XML file using PHP and MySQL
Create Excel file using PHP and MySQL
Create Sitemap using PHP and MySQL
Create RSS Feed using PHP and MySQL
Get / Parse data from XML Sitemap using PHP
Read data from RSS Feed using PHP
Parse data from XML file using PHP