php採用file_get_contents代替使用curl執行個體,curlgetcontents_PHP教程

來源:互聯網
上載者:User

php採用file_get_contents代替使用curl執行個體,curlgetcontents


本文執行個體講述了php採用file_get_contents代替使用curl的方法,分享給大家供大家參考。具體實現方法如下:

file_get_contents代替使用curl其實不多見了,但有時你碰到伺服器不支援curl時我們可以使用file_get_contents代替使用curl,下面看個例子。

當用盡一切辦法發現 伺服器真的無法使用curl時。或者curl不支援https時。curl https 出現502時。你又不想重裝網站環境的時候,你就改用file_get_contents 代替吧。
curl 經常使用的 curl get curl post
curl get 替代 直接用file_get_contents($url) 就可以了
curl post 替代如下:

複製代碼 代碼如下:function Post($url, $post = null) {
$content = http_build_query($post);
$content_length = strlen($content);
$options = array(
'http' => array(
'method' => 'POST',
'header' =>"Content-type: application/x-www-form-urlencoded",
'content' => $post
)
);
return file_get_contents($url, false, stream_context_create($options));
}

希望本文所述對大家的php程式設計有所協助。


php 用file_get_contents與curl都不可以擷取內容

類比個頭部資訊
array( 'method'=>"GET", 'header'=>"User-Agent: ".$_SERVER['HTTP_USER_AGENT']."\r\n" ) ); $context = stream_context_create($opts); $url = dynamic.12306.cn/...0$data = file_get_contents($url,null,$context);echo $data;?> 這樣就可以了

 

用php 的 file_get_contents 或者curl (提出) 及echo (顯示)

純自動錄入,還是配合人手動。假如手動就簡單多了,無非就是寫個正則把頁

面的url和標題搞下來。要是純自動的,你必須寫一段程式類比出來你正常訪問

情況,當你到達第一頁的時候,是發送的一個POST請求,你需要發送相應參數

和POST請求過去,然後再頁面截取相應的需要,當要訪問第二版的時候,發送

的是GET請求,這個時候同理要發送參數過去條狀,直到第十頁。php管網上有

類似的爬蟲類,去下個,很方便的
 

http://www.bkjia.com/PHPjc/907835.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/907835.htmlTechArticlephp採用file_get_contents代替使用curl執行個體,curlgetcontents 本文執行個體講述了php採用file_get_contents代替使用curl的方法,分享給大家供大家參考。具體實...

  • 聯繫我們

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