我收到错误
“503服务暂时不可用”
与我通话
$url = "https://www.okex.com/api/v1/ticker.do?symbol=ltc_btc";
$page = json_decode(file_get_contents($url),true);
var_dump($page);
PHP 文件获取内容
函数,但是当我直接将 url 写入浏览器时,我可以看到该页面,它们是否仅阻止 file_get_contents 函数,或者这是如何工作的?因为如果他们阻止我的 IP,我也无法使用我的浏览器访问该网站,或者?
这是对 APi 服务器的调用,它返回 json。
您的网页更有可能有重定向并且file_get_contents()
无法处理这个问题,但浏览器可以。
所以解决方案是使用curl http://php.net/manual/de/book.curl.php相反,它能够处理这些情况(例如CURLOPT_FOLLOWLOCATION
选项)。
另请参阅此问题:
- get url 重定向时 File_get_contents() 不起作用 https://stackoverflow.com/questions/14517211/file-get-contents-not-working-when-the-get-url-redirects
- PHP cURL 与 file_get_contents https://stackoverflow.com/questions/11064980/php-curl-vs-file-get-contents
这是一个应该可以轻松替换的代码片段(基于官方文档的示例):
function curl_file_get_contents($url)
{
$session = curl_init();
curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($session, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($session, CURLOPT_URL, $url);
$contents = curl_exec($session);
curl_close($session);
return $contents;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)