- // 初始化一個 curl 對象
- $curl = curl_init();
- // 設定你需要抓取的url
- curl_setopt($curl, curlopt_url, 'http://bbs.it-home.org');
- // 設定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);
- ?>
複製代碼例2: post資料sendsms.php,其可以接受兩個表單域,一個是電話號碼,一個是簡訊內容。post資料
- $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);
- ?>
複製代碼例3:使用Proxy 伺服器使用Proxy 伺服器
- $ch = curl_init();
- curl_setopt($ch, curlopt_url, 'http://bbs.it-home.org');
- 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);
- ?>
複製代碼例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); |