关于file_get_contents获取借口403

RT.
有一个接口,本身是302过的

curl -L "api location"  -----> 正确返回数据(预期)
curl "api location"     -----> 返回空(预期)
curl -I "api location"  ------> 头提示302(预期)

如此上面的结论看出,接口是按照预期的想法输出的

由于项目架构的调整,所以才有上面302的产生,但是

php cli方式

var_dump(file_get_contents('api loaction'))-->返回空(非预期)

最后参考stackoverflow:

stackoverflow 1

stackoverflow 2

ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0)'); 
var_dump(file_get_contents('api loaction'))-->返回预期数据(预期)

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 是怎么的差异导致结果不一样?

阅读 5k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题