Retrieve Json Results From Page Using Php

Discussion in 'Web Design and Development' started by SChaput, Nov 12, 2010.

  1. SChaput macrumors regular

    Joined:
    Jul 2, 2008
    Location:
    United States
    #1
    Good Day All,

    Currently I'm using php to read a webpage which is solely a JSON array. The data is similar to this:
    Code:
    {
       "data": [
          {
             "name": "name1",
             "id": "4363523"
          },
          {
             "name": "name2",
             "id": "54725362"
          },
          {
             "name": "name3",
             "id": "646262"
          }
    
         . . .
    
       ]
    }
    
    By using file_get_contents i can retrieve the above information but what is the easiest way to cycle through the entire list? SHould i be able to use some sort of $data['name'][1] or something like that?
    Or is my best bet to use some complicated strpos and substr commands to try and isolate what I need?

    Any help is greatly appreciated. Thanks!
     
  2. phantax macrumors member

    Joined:
    Feb 2, 2009
    #2
    json_decode function should put the data into a multidimensional array which you can then loop through for your keys / vals.
     
  3. Ride9650 macrumors 6502

    Joined:
    Jun 29, 2007
    #3
    To add on to what phantax said, you'll likely have to navigate to the page using "fopen" or "cUrl" since "file_get_contents" returns the information as a string. After that then use "json_decode"
     
  4. phantax macrumors member

    Joined:
    Feb 2, 2009
    #4
    A lot of shared hosting environments disable fopen for remote URLs, so cUrl is your best bet.
     
  5. SChaput thread starter macrumors regular

    Joined:
    Jul 2, 2008
    Location:
    United States
    #5
    Thanks for all your help everyone. I ended up using curl and it seems to have gotten the data in an array however, I was hoping someone could explain more how to call individual fields of the data, here is what the Curl and the json_decode did for me:
    Code:
    function get_data($url) 
    
    { 
    
      $ch = curl_init();
    
      $timeout = 5;
    
      curl_setopt($ch,CURLOPT_URL,$url);
    
      curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    
      curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    
      $data = curl_exec($ch);
    
      curl_close($ch);
    
      return $data;
    
    }
    
    Code:
    $returned_content = get_data('url of a page');
    $stuff = json_decode($returned_content, true);
    print_r($stuff);
    
    Outputs the following:

    Code:
    Array ( [data] => Array ( [0] => Array ( [name] => name1 [id] => 1251 ) [1] => Array ( [name] => name2 [id] => 423423 ) [2] => Array ( [name] => name3 [id] => 42322323 ) 
    
    So how would i cycle through each one of these results to pull, just the ID for example?

    Thanks again!
     
  6. Ride9650 macrumors 6502

    Joined:
    Jun 29, 2007
    #6
    You use whats called a "loop" which in short, will constantly do something a specified number of times.

    I sometimes have a little trouble reading the statements when using print_r on arrays so I apologize ahead of time if this isn't quite right, but it looks like theres just one primary index called "data". Within "data", each index contains another array with your information.

    So to access the ID it'd be something like
    Code:
    $myArray['data'][0]['id']
    Look here under "for" and "foreach" if you're not sure how to loop through everything
     

Share This Page