关于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)

热门话题

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

CakePHP 是一个开源MVC 框架。它使开发、部署和维护应用程序变得更加容易。 CakePHP 有许多库可以减少大多数常见任务的过载。

本教程演示了如何使用PHP有效地处理XML文档。 XML(可扩展的标记语言)是一种用于人类可读性和机器解析的多功能文本标记语言。它通常用于数据存储
