關於file_get_contents擷取借口403

來源:互聯網
上載者:User
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來說,似乎只是這裡影響了。

curlfile_get_contents 的差異,如果不看原始碼的比較方法,應該是使用 tcpdump 等命令去抓一下發出去的請求內容,再做比較。

# tcpdump -i eth0 -A -n -v port ...
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.