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%3A59
HTTP/1.1 301 Moved Permanently
Date: Tue, 09 Feb 2021 00:39:19 GMT
OT-RequestId: 74eadfc7-50a1-4610-83ab-4beab3581621
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Set-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=None
Set-Cookie: OT-Session-Update-Date=1612831159; Path=/; Domain=example.com; Expires=Tue, 09-Feb-2021 01:09:19 GMT; Max-Age=1800; Secure; SameSite=None
Location: https://www.example.com/FetchAvailability?startDate=2021-02-08&endDate=2021-02-08&startTime=00%3A00%3A00&endTime=23%3A59%3A59
Content-Length: 0

postman 处理了 301 跳转.

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

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

参考链接:

  • curl-redirect-requestbody
  • curl 的用法指南

你可能感兴趣的:(curl 301 处理)