利用Php的CURL POST提交表单登录实例详解
例1,CURL使用POST提交XML数据,代码如下:
- $url = "http://www.phpfensi.com";
- <!--?xml version="1.0"?-->
- $ch = curl_init();
- $header[] = "Content-type: text/xml";
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_HEADER, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_close($ch);
在PHP中CURL使用POST提交XML数据时,一定要定义content-type为xml,要不然默认是text/html.
例2,post表单数据,curl是利用URL语法在命令行方式下工作的文件传输工具,代码如下:
- set_time_limit(0);
- @date_default_timezone_set('Asia/Shanghai');
- function curlrequest($url,$postfield,$proxy=""){
- $proxy=trim($proxy);
- $user_agent ="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";
- $ch = curl_init();
- if(!emptyempty($proxy)){
- curl_setopt ($ch, CURLOPT_PROXY, $proxy);
- }
- curl_setopt($ch, CURLOPT_URL, $url);
-
-
- curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postfield);
-
- curl_setopt($ch, CURLOPT_TIMEOUT, 25);
- curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
-
-
-
- curl_setopt($ch,CURLOPT_HTTPHEADER,array(
- 'Accept-Language: zh-cn',
- 'Connection: Keep-Alive',
- 'Cache-Control: no-cache'
- ));
- $document = curl_exec($ch);
- $info=curl_getinfo($ch);
-
- if($info[http_code]=="405"){
- echo "bad proxy {$proxy}n";
- exit;
- }
-
- return $document;
- }
-
- $url="http://example.cn/getInfo.php";
-
- $postfield="userName=test&year=2008&passWord=123456&Submit=%CC%E1%BD%BB";
-
- $proxy = '';
-
- $str=curlrequest($url,$postfield,$proxy);
-
- echo $str;
例3,一个简单利用curl post登录实例.
模拟post登陆提交表单问题
SOOPY类:之前写过一个程序是模拟post来推送一些资源,起初跟大家一样,各种百度谷歌,首先想到的就是用PHP自带的库CURL这个来模拟,自己想偷偷懒看有没有更简单的类来实现呢?还是被我发现了,他就是snoopy类.(中文名史卢比),代码如下:
-
- include("/data/tools/pooy/Snoopy/Snoopy.class.php");
- $snoopy = new Snoopy;
-
- $Parameters["username"] = "user";
- $Parameters["pass"] = "pass";
- $file = "/test/test.jpg";
- $serviceUrl = "http://www.你的地址/fileProcess.php";
- $postfiles["image"] = $file;
- $snoopy->_submit_type = "multipart/form-data";
- $snoopy->submit($serviceUrl,$Parameters,$postfiles);
-
上面这个例子就是实现了一个POST表单提交的案例,由于需求比较复杂,这个snoopy的功能不能满足于我的需求,于是又开始去进攻CURL.
CURL扩展库:这个库是比较成熟的一个扩展库,功能很强大,强大到可以模拟浏览器的任何一个动作,需求是这样子的,第一登陆一个网站后台,第二接口页面,然后开始推送大量资源,这里面的具体逻辑就缩略了,为了操作方便,我把我需要模拟的几个函数封装到了一个类里面,简短代码如下:
-
-
-
-
- class TuisongPost{
-
-
- function TuisongPost(){
-
-
- global $cookie_jar;
- $this->cookie_jar = tempnam('./tmp','cookie');
- $url = "http://www.你的地址";
-
- $post_data = array( "username" => "admin","password" => "admin" );
-
- $ch = curl_init();
-
- curl_setopt($ch, CURLOPT_URL, $url);
-
- curl_setopt($ch, CURLOPT_POST, 1);
-
- curl_setopt($ch, CURLOPT_HEADER, 1);
-
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
-
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
-
- curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_jar);
-
- $output1 = curl_exec($ch);
-
- curl_close($ch);
-
-
- }
-
- function getGid($groupname,$channel,$lanmu){
-
- $url = "http://XXXX.com/creategroup";
-
-
- $data = $this->getGidArr($groupname,$channel,$lanmu);
-
- $ch = curl_init();
-
- $Ref_url = "http://www.你的地址";
-
- curl_setopt($ch, CURLOPT_URL, $url);
-
- curl_setopt($ch, CURLOPT_REFERER, $Ref_url);
-
- curl_setopt($ch, CURLOPT_POST, 1);
-
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-
- curl_setopt($ch, CURLOPT_HEADER, 0);
-
- curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_jar);
-
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-
- $output2 = curl_exec($ch);
-
-
- return $output2;
- curl_close($ch);
-
- }
-
-
- function sendPic($note,$groupid,$groupindex,$img){
-
- $url = "http://XXXX/addimage";
-
- $groupid = intval($groupid);
- $data = $this->sendPicArr($note,$groupid,$groupindex,$img);
-
- $ch = curl_init();
-
- $Ref_url = "http://www.你的地址";
-
- curl_setopt($ch, CURLOPT_URL, $url);
-
- curl_setopt($ch, CURLOPT_REFERER, $Ref_url);
-
- curl_setopt($ch, CURLOPT_POST, 1);
-
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-
- curl_setopt($ch, CURLOPT_HEADER, 0);
-
- curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_jar);
-
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-
- $output2 = curl_exec($ch);
- return $output2 ;
- curl_close($ch);
-
- }
-
-
- function sendMes($url,$img,$imgdesc,$groupid,$groupname,$channel,$lanmu)
- {
-
-
- $url = "http://XXXX/add";
-
- $data = $this->getArr($img,$imgdesc,$groupid,$groupname,$channel,$lanmu);
-
- $ch = curl_init();
-
- $Ref_url = "http://www.你的地址";
-
- curl_setopt($ch, CURLOPT_URL, $url);
-
- curl_setopt($ch, CURLOPT_REFERER, $Ref_url);
-
- curl_setopt($ch, CURLOPT_POST, 1);
-
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-
- curl_setopt($ch, CURLOPT_HEADER, 0);
-
- curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_jar);
-
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-
- $output2 = curl_exec($ch);
-
- curl_close($ch);
-
- }
-
- function getArr($img,$imgdesc,$groupid,$groupname,$channel,$lanmu)
- {
- $post_data = array(
-
-
- "img"=>"@".$img,
- "imgdesc"=>$imgdesc,
- "groupid"=>$groupid,
- "groupname"=>$groupname,
- "channel"=>$channel,
- "lanmu"=>$lanmu,
- "cdate"=>date('Y-m-d')
- );
- return $post_data;
- }
-
- function getGidArr($groupname,$channel,$lanmu)
- {
- $post_data = array(
- "groupname"=>$groupname,
- "channel"=>$channel,
- "lanmu"=>$lanmu,
- "cdate"=>date('Y-m-d')
- );
- return $post_data;
- }
-
- function sendPicArr($note,$groupid,$groupindex,$img)
- {
- $post_data = array(
- "notes"=>$note,
- "id"=>$groupid,
- "index"=>$groupindex,
- "cdate"=>date('Y-m-d'),
-
-
- "img"=>"@".$img
- );
- return $post_data;
- }
-
-
- function unlink($cookie_jar){
- unlink($cookie_jar);
- }
- }
以上就是用CURL来完美解决了这个问题,他能有效的解决cookie存储问题.
|