茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    PHP的cURL庫簡介及使用示例
    來源:易賢網(wǎng) 閱讀:824 次 日期:2015-02-10 13:48:20
    溫馨提示:易賢網(wǎng)小編為您整理了“PHP的cURL庫簡介及使用示例”,方便廣大網(wǎng)友查閱!

    這篇文章主要介紹了PHP的cURL庫簡介及使用示例,需要的朋友可以參考下

    使用PHP的cURL庫可以簡單和有效地去抓網(wǎng)頁。你只需要運(yùn)行一個(gè)腳本,然后分析一下你所抓取的網(wǎng)頁,然后就可以以程序的方式得到你想要的數(shù)據(jù)了。無論是你想從從一個(gè)鏈接上取部分?jǐn)?shù)據(jù),或是取一個(gè)XML文件并把其導(dǎo)入數(shù)據(jù)庫,那怕就是簡單的獲取網(wǎng)頁內(nèi)容,cURL 是一個(gè)功能強(qiáng)大的PHP庫。

    PHP中的CURL函數(shù)庫(Client URL Library Function)

    代碼如下:

    curl_close — 關(guān)閉一個(gè)curl會(huì)話

    curl_copy_handle — 拷貝一個(gè)curl連接資源的所有內(nèi)容和參數(shù)

    curl_errno — 返回一個(gè)包含當(dāng)前會(huì)話錯(cuò)誤信息的數(shù)字編號

    curl_error — 返回一個(gè)包含當(dāng)前會(huì)話錯(cuò)誤信息的字符串

    curl_exec — 執(zhí)行一個(gè)curl會(huì)話

    curl_getinfo — 獲取一個(gè)curl連接資源句柄的信息

    curl_init — 初始化一個(gè)curl會(huì)話

    curl_multi_add_handle — 向curl批處理會(huì)話中添加單獨(dú)的curl句柄資源

    curl_multi_close — 關(guān)閉一個(gè)批處理句柄資源

    curl_multi_exec — 解析一個(gè)curl批處理句柄

    curl_multi_getcontent — 返回獲取的輸出的文本流

    curl_multi_info_read — 獲取當(dāng)前解析的curl的相關(guān)傳輸信息

    curl_multi_init — 初始化一個(gè)curl批處理句柄資源

    curl_multi_remove_handle — 移除curl批處理句柄資源中的某個(gè)句柄資源

    curl_multi_select — Get all the sockets associated with the cURL extension, which can then be "selected"

    curl_setopt_array — 以數(shù)組的形式為一個(gè)curl設(shè)置會(huì)話參數(shù)

    curl_setopt — 為一個(gè)curl設(shè)置會(huì)話參數(shù)

    curl_version — 獲取curl相關(guān)的版本信息

    curl_init()函數(shù)的作用初始化一個(gè)curl會(huì)話,curl_init()函數(shù)唯一的一個(gè)參數(shù)是可選的,表示一個(gè)url地址。

    curl_exec()函數(shù)的作用是執(zhí)行一個(gè)curl會(huì)話,唯一的參數(shù)是curl_init()函數(shù)返回的句柄。

    curl_close()函數(shù)的作用是關(guān)閉一個(gè)curl會(huì)話,唯一的參數(shù)是curl_init()函數(shù)返回的句柄。

    例子一: 基本例子

    代碼如下:

    ﹤?php

    // 初始化一個(gè) cURL 對象

    $curl = curl_init();

    // 設(shè)置你需要抓取的URL

    curl_setopt($curl, CURLOPT_URL, 'http://www.cmx8.cn');

    // 設(shè)置header

    curl_setopt($curl, CURLOPT_HEADER, 1);

    // 設(shè)置cURL 參數(shù),要求結(jié)果保存到字符串中還是輸出到屏幕上。

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    // 運(yùn)行cURL,請求網(wǎng)頁

    $data = curl_exec($curl);

    // 關(guān)閉URL請求

    curl_close($curl);

    // 顯示獲得的數(shù)據(jù)

    var_dump($data);

    ?>

    例子二: POST數(shù)據(jù)

    sendSMS.php,其可以接受兩個(gè)表單域,一個(gè)是電話號碼,一個(gè)是短信內(nèi)容。

    代碼如下:

    ﹤?php

    $phoneNumber = '13812345678';

    $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.lxvoip.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);

    ?﹥

    例子三:使用代理服務(wù)器

    代碼如下:

    ﹤?php 

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 'http://www.cmx8.cn');

    curl_setopt($ch, CURLOPT_HEADER, 1);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);

    curl_setopt($ch, CURLOPT_PROXY, 'proxy.lxvoip.com:1080');

    curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:password');

    $data = curl_exec();

    curl_close($ch);

    ?﹥

    例子四: 模擬登錄

    Curl 模擬登錄 discuz 程序,適合DZ7.0,將username改成你的用戶名,userpass改成你的密碼就可以了.

    代碼如下:

    <?php

    /**

    * Curl 模擬登錄 discuz 程序

    * 尚未實(shí)現(xiàn)開啟驗(yàn)證碼的的論壇登錄功能

    */

    !extension_loaded('curl') && die('The curl extension is not loaded.');

    $discuz_url = 'http://www.lxvoip.com';//論壇地址

    $login_url = $discuz_url .'/logging.php?action=login';//登錄頁地址

    $get_url = $discuz_url .'/my.php?item=threads'; //我的帖子

    $post_fields = array();

    //以下兩項(xiàng)不需要修改

    $post_fields['loginfield'] = 'username';

    $post_fields['loginsubmit'] = 'true';

    //用戶名和密碼,必須填寫

    $post_fields['username'] = 'lxvoip';

    $post_fields['password'] = '88888888';

    //安全提問

    $post_fields['questionid'] = 0;

    $post_fields['answer'] = '';

    //@todo驗(yàn)證碼

    $post_fields['seccodeverify'] = '';

    //獲取表單FORMHASH

    $ch = curl_init($login_url);

    curl_setopt($ch, CURLOPT_HEADER, 0);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $contents = curl_exec($ch);

    curl_close($ch);

    preg_match('/<input\s*type="hidden"\s*name="formhash"\s*value="(.*?)"\s*\/>/i', $contents, $matches);

    if(!empty($matches)) {

    $formhash = $matches[1];

    } else {

    die('Not found the forumhash.');

    }

    //POST數(shù)據(jù),獲取COOKIE

    $cookie_file = dirname(__FILE__) . '/cookie.txt';

    //$cookie_file = tempnam('/tmp');

    $ch = curl_init($login_url);

    curl_setopt($ch, CURLOPT_HEADER, 0);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_POST, 1);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);

    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);

    curl_exec($ch);

    curl_close($ch);

    //帶著上面得到的COOKIE獲取需要登錄后才能查看的頁面內(nèi)容

    $ch = curl_init($get_url);

    curl_setopt($ch, CURLOPT_HEADER, 0);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);

    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);

    $contents = curl_exec($ch);

    curl_close($ch);

    var_dump($contents);

    ?>

    以上就是本文的全部內(nèi)容了,希望大家能夠喜歡。

    更多信息請查看IT技術(shù)專欄

    更多信息請查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機(jī)網(wǎng)站地址:PHP的cURL庫簡介及使用示例
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2026上岸·考公考編培訓(xùn)報(bào)班

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)