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