当前位置: 博客首页 >> PHP >> 阅读正文

学习CURL之简单实现中国贸易网的登录与信息发送

作者: 郑晓 分类: PHP 发布于: 2012-06-28 08:21 浏览:11,257 没有评论


学习CURL之简单实现中国贸易网的登录与信息发送,最近在做第三方平台的登录与信息同步功能,所以简单学习了下php的curl扩展库,下面是用curl简单实现中国贸易网的登录与供应信息的同步发送类,过程还算比较简单的,第一个方法用curl实现登录,第二个方法是使用第一步得到的cookies实现信息的发送,算是入门级的…. 发布信息时的分类目前是写死的,怎么传还是要靠大家了… <?php $cntrades = new Cntrades; $cntrades->login('username','password'); //账号密码 $cntrades->post('这里是标题','这里是内容'); //发送的供应信息标题和内容</p> <p>class Cntrades { var $cookieJar= "d:/wwwroot/test.com/cntradecookies.txt"; //定义cookie文件 这里文件名是写死的,也可以用tmpfile函数建立临时文件 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: <a href="http://www.cntrades.com">http://www.cntrades.com</a>"); //传递的header头信息 var $login_url = "<a href="http://www.cntrades.com/member/login.php">http://www.cntrades.com/member/login.php</a>"; //登录地址 var $post_url = "<a href="http://www.cntrades.com/member/my.php?mid=5&action=add">http://www.cntrades.com/member/my.php?mid=5&action=add</a>" ; //发布信息地址 var $forward = "<a href="http://www.cntrades.com/">http://www.cntrades.com/</a>"; </p> <pre><code>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) { /*模拟供应发布表单*/ $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' ; //很多字段需要传递,过于复杂就不弄了 这里分类等信息是写死的 $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); //是否抓取跳转后的页面 curl_setopt($ch,CURLOPT_HTTPHEADER,$this->headerArr); $re = curl_exec($ch); print_r(curl_getinfo($ch)); echo $re; curl_close($ch); }

} ?>

       

本文采用知识共享署名-非商业性使用 3.0 中国大陆许可协议进行许可,转载时请注明出处及相应链接。

本文永久链接: https://www.zh30.com/chinese-trade-network-login-information-sent-to-learn-the-simple-implementation-of-curl.html

发表评论

change vcode