-2

I need to parse JSON from multiple URLs. Here is the way that I'm following:

<?php
//call
$url1 = file_get_contents("https://www.url1.com");
$url2 = file_get_contents("https://www.url2.com");
$url3 = file_get_contents("https://www.url3.com");
$url4 = file_get_contents("https://www.url4.com");
$url5 = file_get_contents("https://www.url5.com");
//parse
$decode1 = json_decode($url1, true);
$decode2 = json_decode($url2, true);
$decode3 = json_decode($url3, true);
$decode4 = json_decode($url4, true);
$decode5 = json_decode($url5, true);

//echo 
if (is_array($decode1)) {
                foreach ($decode1 as $key => $value) {
                    if (is_array($value) && isset($value['price'])) {
                        $price = $value['price'];
                        echo '<span><b>' . $price . '</b><span>';
                    }
                }
            }
?>

This way causes slowness in the page openings. On the other hand, I get these errors:

Warning: file_get_contents(https://www.url1.com): failed to open stream: Redirection limit reached, aborting in /home/directory/public_html/file.php on line 12

Warning: file_get_contents(https://www.url2.com): failed to open stream: Redirection limit reached, aborting in /home/directory/public_html/file.php on line 13

etc.

How can I fix the redirection limit reached warning?

Madame Green Pea
  • 187
  • 1
  • 3
  • 13
  • 1
    It seems like you are setting the value of `$context` twice. You are not showing us what `$opts` is in your example, so perhaps that is incorrect, because that is what your $context is being set to. – bronkula Dec 25 '18 at 21:32
  • Thanks for the point out. I forgot to delete `$context = stream_context_create($opts);` which was from the previous alternative solution. It was related to header options. – Madame Green Pea Dec 25 '18 at 21:53

1 Answers1

1

I would suggest using cURL for fetching remote data. You could do this:

$urls = [
    "https://www.url1.com",
    "https://www.url2.com",
    "https://www.url3.com",
    "https://www.url4.com",
    "https://www.url5.com"
  ];
$decoded = array_map("loadJSON", $urls);

if (is_array($decoded[0])) {
  foreach ($decoded[0] as $key => $value) {
    if (is_array($value) && isset($value['price'])) {
      $price = $value['price'];
      echo '<span><b>' . $price . '</b><span>';
    }
  }
}

/**
 * Downloads a JSON file from a URL and returns its decoded content
 */
function loadJSON($url) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // If your server does not have SSL
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // Follow redirections
  curl_setopt($ch, CURLOPT_MAXREDIRS, 10); // 10 max redirections
  $content = curl_exec($ch);
  curl_close($ch);
  $res = json_decode($content, true);
  return $res;
}
blex
  • 24,941
  • 5
  • 39
  • 72
  • Thank you for the suggestion. I need to parse JSON files one by one. When I echo the result, it gives all of the results. Also, would you please let me know why you suggest cURL? What is better than file_get_contents function? – Madame Green Pea Dec 25 '18 at 22:05
  • @MadameGreenPea cURL gives you a lot more options for headers ([see here](https://stackoverflow.com/questions/11064980/php-curl-vs-file-get-contents)), I edited my answer to follow redirects. As for displaying the files' contents one by one, I edited my answer to do only the first one (`$decoded[0]`, and if you want the URL, it will be `$urls[0]`) – blex Dec 25 '18 at 22:15
  • Thank you so much! @blex – Madame Green Pea Dec 25 '18 at 22:31