php curl_init函數用法舉例

來源:互聯網
上載者:User
  1. // 初始化一個 curl 對象
  2. $curl = curl_init();
  3. // 設定你需要抓取的url
  4. curl_setopt($curl, curlopt_url, 'http://bbs.it-home.org');
  5. // 設定header
  6. curl_setopt($curl, curlopt_header, 1);
  7. // 設定curl 參數,要求結果儲存到字串中還是輸出到螢幕上。
  8. curl_setopt($curl, curlopt_returntransfer, 1);
  9. // 運行curl,請求網頁
  10. $data = curl_exec($curl);
  11. // 關閉url請求
  12. curl_close($curl);
  13. // 顯示獲得的資料
  14. var_dump($data);
  15. ?>
複製代碼

例2: post資料sendsms.php,其可以接受兩個表單域,一個是電話號碼,一個是簡訊內容。post資料

  1. $phonenumber = '13812345678';
  2. $message = 'this message was generated by curl and php';
  3. $curlpost = 'pnumber=' . urlencode($phonenumber) . '&message=' . urlencode($message) . '&submit=send';
  4. $ch = curl_init();
  5. curl_setopt($ch, curlopt_url, 'http://www.lxvoip.com/sendsms.php');
  6. curl_setopt($ch, curlopt_header, 1);
  7. curl_setopt($ch, curlopt_returntransfer, 1);
  8. curl_setopt($ch, curlopt_post, 1);
  9. curl_setopt($ch, curlopt_postfields, $curlpost);
  10. $data = curl_exec();
  11. curl_close($ch);
  12. ?>
複製代碼

例3:使用Proxy 伺服器使用Proxy 伺服器

  1. $ch = curl_init();
  2. curl_setopt($ch, curlopt_url, 'http://bbs.it-home.org');
  3. curl_setopt($ch, curlopt_header, 1);
  4. curl_setopt($ch, curlopt_returntransfer, 1);
  5. curl_setopt($ch, curlopt_httpproxytunnel, 1);
  6. curl_setopt($ch, curlopt_proxy, 'proxy.lxvoip.com:1080');
  7. curl_setopt($ch, curlopt_proxyuserpwd, 'user:password');
  8. $data = curl_exec();
  9. curl_close($ch);
  10. ?>
複製代碼

例4: 類比登入curl 類比登入 discuz 程式,適合dz7.0,將username改成你的使用者名稱,userpass改成你的密碼就可以了.curl 類比登入 discuz 程式

!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(); //以下兩項不需要修改 $post_fields['loginfield'] = 'username'; $post_fields['loginsubmit'] = 'true'; //使用者名稱和密碼,必須填寫 $post_fields['username'] = 'lxvoip'; $post_fields['password'] = '88888888'; //安全提問 $post_fields['questionid'] = 0; $post_fields['answer'] = ''; //@todo驗證碼 $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('//i', $contents, $matches); if(!empty($matches)) { $formhash = $matches[1]; } else { die('not found the forumhash.'); }

//post資料,擷取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擷取需要登入後才能查看的頁面內容 $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);

  • 聯繫我們

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