php file_get_contents偽造user_agent curl設定

來源:互聯網
上載者:User
php程式提供的兩個函數file_get_contents 和 curl,在抓取遠程內容或類比瀏覽器訪問的時候相當有用。不過一些網站出於安全等考慮會根據來訪ip是否攜帶user_agent資訊來判斷是正常的瀏覽器用戶端還是機器,如果是機器則拒絕訪問。所以,我們的就需要對file_get_contents和curl偽造user_agent。下面給大家詳解一下如何file_get_contents偽造user_agent。  

源碼 列印 協助
01 <?php
02 function curl_get_file_contents($URL){
03     $c = curl_init();
04     curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
05     //curl_setopt($c, CURLOPT_HEADER, 1);//輸出遠程伺服器的header資訊
06     curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95
07  
08 Safari/537.36 SE 2.X MetaSr 1.0');
09

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.