我得到了错误
“503服务暂时不可用”
我的电话
$url = "https://www.okex.com/api/v1/ticker.do?symbol=ltc_btc";
$page = json_decode(file_get_contents($url),true);
var_dump($page);
Run Code Online (Sandbox Code Playgroud)
PHP file_get_contents
函数,但是当我将 url 直接写入浏览器时,我可以看到该页面,它们是仅阻止 file_get_contents 函数还是它是如何工作的?因为如果他们阻止了我的 ip,我也无法使用浏览器访问该站点,或者?
这是对 APi 服务器的调用,它返回给我 json。
更有可能的是您的网页具有重定向并且file_get_contents()无法处理,但浏览器可以。
所以解决方案是curl改用,它能够处理这些情况(例如,使用 CURLOPT_FOLLOWLOCATION 选项)。
另请参阅以下问题:
这是一个可以作为简单替换的片段(基于官方文档的示例):
function curl_get_file_contents($URL)
{
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($c, CURLOPT_URL, $URL);
$contents = curl_exec($c);
curl_close($c);
if ($contents) return $contents;
else return FALSE;
}
Run Code Online (Sandbox Code Playgroud)