當(dāng)前位置:博客首頁(yè)>>PHP >> 閱讀正文

學(xué)習(xí)CURL之簡(jiǎn)單實(shí)現(xiàn)中國(guó)貿(mào)易網(wǎng)的登錄與信息發(fā)送

作者: 鄭曉 分類: PHP 發(fā)布于: 2012-06-28 08:21 瀏覽:10,913 沒(méi)有評(píng)論


學(xué)習(xí)CURL之簡(jiǎn)單實(shí)現(xiàn)中國(guó)貿(mào)易網(wǎng)的登錄與信息發(fā)送,最近在做第三方平臺(tái)的登錄與信息同步功能,所以簡(jiǎn)單學(xué)習(xí)了下php的curl擴(kuò)展庫(kù),下面是用curl簡(jiǎn)單實(shí)現(xiàn)中國(guó)貿(mào)易網(wǎng)的登錄與供應(yīng)信息的同步發(fā)送類,過(guò)程還算比較簡(jiǎn)單的,第一個(gè)方法用curl實(shí)現(xiàn)登錄,第二個(gè)方法是使用第一步得到的cookies實(shí)現(xiàn)信息的發(fā)送,算是入門(mén)級(jí)的….

發(fā)布信息時(shí)的分類目前是寫(xiě)死的,怎么傳還是要靠大家了…

login('username','password'); //賬號(hào)密碼
$cntrades->post('這里是標(biāo)題','這里是內(nèi)容'); //發(fā)送的供應(yīng)信息標(biāo)題和內(nèi)容

class Cntrades {
var $cookieJar= "d:/wwwroot/test.com/cntradecookies.txt"; //定義cookie文件 這里文件名是寫(xiě)死的,也可以用tmpfile函數(shù)建立臨時(shí)文件
var $headerArr = array("User-Agent: Mozilla/5.0 (Windows NT 5.2) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11","Referer: http://www.cntrades.com"); //傳遞的header頭信息
var $login_url = "http://www.cntrades.com/member/login.php"; //登錄地址
var $post_url = "http://www.cntrades.com/member/my.php?mid=5&action=add" ; //發(fā)布信息地址
var $forward = "http://www.cntrades.com/";

function login($username,$password) {
/*模擬登錄表單*/
$data['forward'] = $this->forward;
$data['username'] = $username;
$data['password'] = $password;
$data['cookietime'] = "2592000";
$data['goto'] = '1';
$data['submit'] =' 登 錄 ';
$data = http_build_query($data,'&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$this->login_url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_COOKIEJAR,$this->cookieJar);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$this->headerArr);
$rs = curl_exec($ch);
curl_close($ch);
unset($ch);
unset($data);
echo strpos($rs,'DZg_auth')?'登錄成功!':'登錄失?。?;
}

function post($title,$content) {
/*模擬供應(yīng)發(fā)布表單*/
$data['action'] = 'add';
$data['mid'] = 5;
$data['itemid'] = 0;
$data['forward'] = $this->forward;
$data['style'] = '';
$data['post[typeid]'] = 0 ;
$data['post[typeid]'] = 0 ;
$data['post[title]'] = $title ;
$data['post[catid]'] = '533' ; //很多字段需要傳遞,過(guò)于復(fù)雜就不弄了 這里分類等信息是寫(xiě)死的
$data['post[content]'] = $content;
$data['submit'] = " 提 交 ";
$data = http_build_query($data, '&'); //application/x-www-form-urlencoded OR multipart/form-data
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$this->post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_COOKIEFILE,$this->cookieJar);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); //是否抓取跳轉(zhuǎn)后的頁(yè)面
curl_setopt($ch,CURLOPT_HTTPHEADER,$this->headerArr);
$re = curl_exec($ch);
print_r(curl_getinfo($ch));
echo $re;
curl_close($ch);
}
}
?>

? ? ? ?

本文采用知識(shí)共享署名-非商業(yè)性使用 3.0 中國(guó)大陸許可協(xié)議進(jìn)行許可,轉(zhuǎn)載時(shí)請(qǐng)注明出處及相應(yīng)鏈接。

本文永久鏈接: http://yjfs.org.cn/chinese-trade-network-login-information-sent-to-learn-the-simple-implementation-of-curl.html

發(fā)表評(píng)論

change vcode