环球讯息:php curl 301跳转怎么处理

时间:2023-02-03 10:57:29       来源:PHP中文网


(资料图片)

本教程操作环境:Windows10系统、PHP8.1版、DELL G3电脑

php curl 301跳转怎么处理?

curl 301 处理

我在处理某个接口调用时发现postman能正常响应数据,但是程序运行时却获取不到正常数据响应.

排查日志发现代码里curl时会返回301, 所以会导致接口未能正确获取到响应数据.

curl -i http://www.example.com/fetchAvailability\?startDate\=2021-02-08\&endDate\=2021-02-08\&startTime\=00%3A00%3A00\&endTime\=23%3A59%3A59HTTP/1.1 301 Moved PermanentlyDate: Tue, 09 Feb 2021 00:39:19 GMTOT-RequestId: 74eadfc7-50a1-4610-83ab-4beab3581621X-Content-Type-Options: nosniffX-XSS-Protection: 1; mode=blockSet-Cookie: OT-SessionId=b1db19cb-00cf-47be-95ac-33eee1172706; Path=/; Domain=example.com; Expires=Tue, 09-Feb-2021 01:09:19 GMT; Max-Age=1800; Secure; SameSite=NoneSet-Cookie: OT-Session-Update-Date=1612831159; Path=/; Domain=example.com; Expires=Tue, 09-Feb-2021 01:09:19 GMT; Max-Age=1800; Secure; SameSite=NoneLocation: https://www.example.com/FetchAvailability?startDate=2021-02-08&endDate=2021-02-08&startTime=00%3A00%3A00&endTime=23%3A59%3A59Content-Length: 0
登录后复制

postman 处理了 301 跳转.

php curl里如果要处理301 跳转的问题,需要加上 下面这行代码:

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
登录后复制

推荐学习:《PHP视频教程》

以上就是php curl 301跳转怎么处理的详细内容,更多请关注php中文网其它相关文章!

关键词: 相关文章 视频教程 程序运行