PHP file_get_contents()返回启用cookie?

Joh*_*ith -1 php get http file-get-contents

我正在尝试file_get_contents()https://satoshidice.com/api/status上执行此操作并将其返回 - http://puu.sh/58lCb

print file_get_contents($this->base . 'status');
Run Code Online (Sandbox Code Playgroud)

$this->base 是satoshidice.com/api(带有http).

为什么它会返回上面张贴的图片中的内容..?

All*_*hak 6

建议你使用cURL而不是file_get_contents

$_curl = curl_init();
curl_setopt($_curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($_curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($_curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($_curl, CURLOPT_COOKIEFILE, './cookiePath.txt');
curl_setopt($_curl, CURLOPT_COOKIEJAR, './cookiePath.txt');
curl_setopt($_curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; InfoPath.1)');
curl_setopt($_curl, CURLOPT_FOLLOWLOCATION, true); //new added
curl_setopt($_curl, CURLOPT_URL, $url);
$rtn = curl_exec( $_curl );
Run Code Online (Sandbox Code Playgroud)

更新:添加'CURLOPT_FOLLOWLOCATION'

说明:

对于此主机,它将检查cookie是否存在.

如果请求的cookie不存在,它将创建cookie并生成HTTP代码307以重定向:

https://satoshidice.com/api/status?stage=1