1. 程式人生 > >php cURL庫

php cURL庫

Curl 模擬登入 discuz 程式,適合DZ7.0,將username改成你的使用者名稱,userpass改成你的密碼就可以了.
Curl 模擬登入 discuz 程式
<?php   
  
  
!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('/<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資料,獲取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);