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 是怎麼的差異導致結果不一樣?
回複內容:
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 是怎麼的差異導致結果不一樣?
這裡是不是沒說清楚?OP這裡 file_get_contents 的時候也是返回403嗎?
如果僅僅是按照下面改UA來說,似乎只是這裡影響了。
curl 與 file_get_contents 的差異,如果不看原始碼的比較方法,應該是使用 tcpdump 等命令去抓一下發出去的請求內容,再做比較。
# tcpdump -i eth0 -A -n -v port ...