关于file_get_contents获取借口403
RT.
有一个接口,本身是302过的
<code>curl -L "api location" -----> 正确返回数据(预期) curl "api location" -----> 返回空(预期) curl -I "api location" ------> 头提示302(预期) </code>
如此上面的结论看出,接口是按照预期的想法输出的
由于项目架构的调整,所以才有上面302的产生,但是
php cli方式
<code>var_dump(file_get_contents('api loaction'))-->返回空(非预期) </code>
最后参考stackoverflow:
stackoverflow 1
stackoverflow 2
<code>ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0)'); var_dump(file_get_contents('api loaction'))-->返回预期数据(预期) </code>
stackoverflow上的解释:
This is not a problem with your script, but with the resource you are requesting. The web server is returning the "forbidden" status code.
It could be that it blocks PHP scripts to prevent scraping, or your IP if you have made too many requests.
You should probably talk to the administrator of the remote server.
最后问题来了:
既然server端做了处理,那么curl和file_get_contents 是怎么的差异导致结果不一样?
回复内容:
RT.
有一个接口,本身是302过的
<code>curl -L "api location" -----> 正确返回数据(预期) curl "api location" -----> 返回空(预期) curl -I "api location" ------> 头提示302(预期) </code>
如此上面的结论看出,接口是按照预期的想法输出的
由于项目架构的调整,所以才有上面302的产生,但是
php cli方式
<code>var_dump(file_get_contents('api loaction'))-->返回空(非预期) </code>
最后参考stackoverflow:
stackoverflow 1
stackoverflow 2
<code>ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0)'); var_dump(file_get_contents('api loaction'))-->返回预期数据(预期) </code>
stackoverflow上的解释:
This is not a problem with your script, but with the resource you are requesting. The web server is returning the "forbidden" status code.
It could be that it blocks PHP scripts to prevent scraping, or your IP if you have made too many requests.
You should probably talk to the administrator of the remote server.
最后问题来了:
既然server端做了处理,那么curl和file_get_contents 是怎么的差异导致结果不一样?
这里是不是没说清楚?OP这里 file_get_contents
的时候也是返回403吗?
如果仅仅是按照下面改UA来说,似乎只是这里影响了。
curl
与 file_get_contents
的差异,如果不看源代码的比较方法,应该是使用 tcpdump
等命令去抓一下发出去的请求内容,再做比较。
<code># tcpdump -i eth0 -A -n -v port ... </code>

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發
