php curl 301跳轉的處理方法:1、打開相應的php文件;2、通過添加代碼為“curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);”處理301跳轉即可。
本教程操作環(huán)境:Windows10系統(tǒng)、PHP8.1版、DELL G3電腦
php curl 301跳轉怎么處理?
curl 301 處理
我在處理某個接口調用時發(fā)現(xiàn)postman能正常響應數(shù)據(jù),但是程序運行時卻獲取不到正常數(shù)據(jù)響應.
排查日志發(fā)現(xiàn)代碼里curl時會返回301, 所以會導致接口未能正確獲取到響應數(shù)據(jù).
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);
登錄后復制
推薦學習:《PHP視頻教程》