Heim  >  Artikel  >  Backend-Entwicklung  >  PHP施用cURL代理

PHP施用cURL代理

WBOY
WBOYOriginal
2016-06-13 10:56:101117Durchsuche

PHP使用cURL代理
使用PHP 的cURL 庫可以簡單和有效地去抓網頁。你只需要運行一個腳本,然後分析一下你所抓取的網頁,然後就可以以程序的方式得到你想要的數據了。無論是你想從從一個鏈接上取部分數據,或是取一個XML 文件並把其導入數據庫,那怕就是簡單的獲取網頁內容,cURL 是一個功能強大的PHP 庫。本文主要講述如果使用這個PHP 庫。

啟用 cURL 設置

首先,我們得先要確定我們的PHP 是否開啟了這個庫,你可以通過使用php_info() 函數來得到這一信息。
Php代碼

    ﹤?php 
    phpinfo(); 
    ?﹥ 

如果你可以在網頁上看到下面的輸出,那麽表示cURL 庫已被開啟。

如果你看到的話,那麽你需要設置你的PHP 並開啟這個庫。如果你是在Windows 平臺下,那麽非常簡單,你需要改一改你的php.ini 文件的設置,找到php_curl.dll ,並取消前面的分號註釋就行了。如下所示:
Php代碼

    //取消下在的註釋 
    extension=php_curl.dll 



如果你是在Linux 下面,那麽,你需要重新編譯你的PHP 了,編輯時,你需要打開編譯參數—— 在configure 命令上加上“–with-curl” 參數。

一個小示例

如果一切就緒,下面是一個小例程:
Php代碼

    ﹤?php 
    // 初始化一個 cURL 對象 
    $curl = curl_init();  
    // 設置你需要抓取的URL 
    curl_setopt($curl, CURLOPT_URL, 'http://cocre.com'); 
    // 設置header 
    curl_setopt($curl, CURLOPT_HEADER, 1); 
    // 設置cURL 參數,要求結果保存到字符串中還是輸出到屏幕上。 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    // 運行cURL,請求網頁 
    $data = curl_exec($curl); 
    // 關閉URL請求 
    curl_close($curl); 
    // 顯示獲得的數據 
    var_dump($data); 





如何POST 數據

上面是抓取網頁的代碼,下面則是向某個網頁POST 數據。假設我們有一個處理表單的網址http://www.example.com/sendSMS.php ,其可以接受兩個表單域,一個是電話號碼,一個是短信內容。
Php代碼

    ﹤?php 
    $phoneNumber = '13912345678'; 
    $message = 'This message was generated by curl and php'; 
    $curlPost = 'pNUMBER='  . urlencode($phoneNumber) . '&MESSAGE=' . urlencode($message) . '&SUBMIT=Send'; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/sendSMS.php'); 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); 
    $data = curl_exec(); 
    curl_close($ch); 
    ?﹥ 



從上面的程序我們可以看到,使用CURLOPT_POST 設置HTTP 協議的POST 方法,而不是GET 方法,然後以CURLOPT_POSTFIELDS 設置POST 的數據。

關於代理服務器

下面是一個如何使用代理服務器的示例。請註意其中高亮的代碼,代碼很簡單,我就不用多說了。
Php代碼

    ﹤?php  
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, 'http://www.example.com'); 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); 
    curl_setopt($ch, CURLOPT_PROXY, 'fakeproxy.com:1080'); 
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:password'); 
    $data = curl_exec(); 
    curl_close($ch); 
    ?﹥ 





關於SSL 和Cookie

關於SSL 也就是HTTPS 協議,你只需要把CURLOPT_URL 連接中的http:// 變成https:// 就可以了。當然,還有一個參數叫CURLOPT_SSL_VERIFYHOST 可以設置為驗證站點。

關於Cookie ,你需要了解下面三個參數:

CURLOPT_COOKIE ,在當面的會話中設置一個cookie

CURLOPT_COOKIEJAR ,當會話結束的時候保存一個Cookie

CURLOPT_COOKIEFILE ,Cookie 的文件。

HTTP 服務器認證

最後,我們來看一看HTTP 服務器認證的情況。
Php代碼

    ﹤?php  
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, 'http://www.example.com'); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
    curl_setopt(CURLOPT_USERPWD, '[username]:[password]') 
    $data = curl_exec(); 
    curl_close($ch); 
    ?﹥


总结,cURL关于http可以做三件事:
1,直接抓取某个地址的网页。
2,向某个地址post数据。
3,经过代理访问某个网页。也可以post。
其他,还有https和cookie。


關於其它更多的內容,請參看相關的cURL 手冊。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn