【发布时间】:2019-05-24 08:09:31
【问题描述】:
我需要从多个 URL 解析 JSON。这是我遵循的方式:
<?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>';
}
}
}
?>
这种方式会导致页面打开速度变慢。另一方面,我收到以下错误:
警告:file_get_contents(https://www.url1.com):打开失败 流:达到重定向限制,正在中止 /home/directory/public_html/file.php 第 12 行
警告:file_get_contents(https://www.url2.com):打开失败 流:达到重定向限制,正在中止 /home/directory/public_html/file.php 在第 13 行
等等。
如何解决redirection limit reached 警告?
【问题讨论】:
-
您似乎将
$context的值设置了两次。您没有向我们展示您的示例中的$opts是什么,所以这可能是不正确的,因为这就是您的 $context 设置的内容。 -
感谢您的指出。我忘了删除来自先前替代解决方案的
$context = stream_context_create($opts);。它与标题选项有关。