We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 43374
    • 39 Posts
    Hi is there a way I can import JSON data from an external URL. Unfortunately I am not experienced with php.

    My goal is to import JSON data from the itunes search api and parse it to my site.

    Example for iTunes JSON data
    Link: http://itunes.apple.com/search?term=Avatar+Cameron&entity=iPadSoftware&country=DE&limit=1

    Snippet code I am using
    $string=file_get_contents('http://itunes.apple.com/search?term=Avatar+Cameron&entity=iPadSoftware&country=DE&limit=1');
    $json_o=json_decode($string);
    $json_a=json_decode($string,true);
    // associative array mode
    echo $json_a[artworkUrl60];


    Error message i receive but unfortunately dont understand:
    Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in ../core/cache/includes/elements/modsnippet/2.include.cache.php on line 7

    Warning: file_get_contents(http://itunes.apple.com/search?term=Avatar+Cameron&entity=iPadSoftware&country=DE&limit=1) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in ../core/cache/includes/elements/modsnippet/2.include.cache.php on line 7

    This question has been answered by BobRay. See the first response.

    • You probably want to use cURL.
      • discuss.answer
        • 3749
        • 24,544 Posts
        This is the key message:

        URL file-access is disabled in the server configuration

        You might ask your host if they will enable it for you. If not, cURL is probably your best bet.

        Something like this:

        $ch = curl_init('http://itunes.apple.com/search?term=Avatar+Cameron&entity=iPadSoftware&country=DE&limit=1');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200);
        $string = curl_exec($ch);
        $curl_errno = curl_errno($ch);
        $curl_error = curl_error($ch);
        curl_close($ch);
        
        if ($curl_errno > 0) {
           echo "cURL Error ($curl_errno): $curl_error\n";
        } else {
           echo "Data received: $data\n";
          /* decode JSON here */
        }
        
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 43374
          • 39 Posts
          Thanks guys. Espacialy for the detailed example! Works like a charm!

          If someone later stumbles over this, here is how i finaly did it with the awesome help:
          <?php
          $ch = curl_init('http://itunes.apple.com/search?term=Avatar+Cameron&entity=iPadSoftware&country=DE&limit=5');
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
          curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200);
          $string = curl_exec($ch);
          $curl_errno = curl_errno($ch);
          $curl_error = curl_error($ch);
          curl_close($ch);
           
          $arr = json_decode($string,true);
          
          foreach($arr['results'] as $val)
          {
          echo "some html...";
          echo $val['trackViewUrl'];
          echo "more html...";
          echo $val['artworkUrl60'];
          echo "and more html...";
          }
          [ed. note: sh0ck23 last edited this post 10 years, 11 months ago.]
            • 3749
            • 24,544 Posts
            I'm glad it worked for you. Thanks for reporting back. smiley
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting