Home  >  Article  >  Backend Development  >  Detailed explanation of using Php's CURL POST to submit form login examples_PHP tutorial

Detailed explanation of using Php's CURL POST to submit form login examples_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:48:371479browse

I have talked a lot about the php curl function to implement post submission data. Now I will introduce to you one way to submit xml and one way to submit form data.


Example 1

CURL uses POST to submit XML data

The code is as follows Copy code
 代码如下 复制代码

$url = "http://www.bKjia.c0m";
 
 $ch = curl_init();
$header[] = "Content-type: text/xml";//定义content-type为xml
curl_setopt($ch, CURLOPT_URL, $url); //定义表单提交地址
curl_setopt($ch, CURLOPT_POST, 1);   //定义提交类型 1:POST ;0:GET
curl_setopt($ch, CURLOPT_HEADER, 1); //定义是否显示状态头 1:显示 ; 0:不显示
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//定义请求类型
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);//定义是否直接输出返回流
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //定义提交的数据,这里是XML文件
 curl_close($ch);//关闭

$url = "http://www.bKjia.c0m";

$ch = curl_init();

$header[] = "Content-type: text/xml";//Define content-type as xml curl_setopt($ch, CURLOPT_URL, $url); //Define form submission address curl_setopt($ch, CURLOPT_POST, 1); //Define submission type 1: POST; 0: GET

curl_setopt($ch, CURLOPT_HEADER, 1); //Define whether to display the status header 1: display; 0: not display

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//Define request type
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); //Define whether to directly output the return stream

curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //Define the submitted data, here is the XML file
 代码如下 复制代码
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(); // 初始化CURL句柄
if(!empty($proxy)){
curl_setopt ($ch, CURLOPT_PROXY, $proxy);//设置代理服务器
}
curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL
//curl_setopt($ch, CURLOPT_FAILONERROR, 1); // 启用时显示HTTP状态码,默认行为是忽略编号小于等于400的HTTP信息
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//启用时会将服务器服务器返回的“Location:”放在header中递归的返回给服务器
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);// 设为TRUE把curl_exec()结果转化为字串,而不是直接输出
curl_setopt($ch, CURLOPT_POST, 1);//启用POST提交
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfield); //设置POST提交的字符串
//curl_setopt($ch, CURLOPT_PORT, 80); //设置端口
curl_setopt($ch, CURLOPT_TIMEOUT, 25); // 超时时间
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);//HTTP请求User-Agent:头
//curl_setopt($ch,CURLOPT_HEADER,1);//设为TRUE在输出中包含头信息
//$fp = fopen("example_homepage.txt", "w");//输出文件
//curl_setopt($ch, CURLOPT_FILE, $fp);//设置输出文件的位置,值是一个资源类型,默认为STDOUT (浏览器)。
curl_setopt($ch,CURLOPT_HTTPHEADER,array(
'Accept-Language: zh-cn',
'Connection: Keep-Alive',
'Cache-Control: no-cache'
));//设置HTTP头信息
$document = curl_exec($ch); //执行预定义的CURL
$info=curl_getinfo($ch); //得到返回信息的特性
//print_r($info);
if($info[http_code]=="405"){
echo "bad proxy {$proxy}n"; //代理出错
exit;
}
//curl_close($ch);
return $document;
}
//请求URL
$url="http://example.cn/getInfo.php";
//POST提交数据,可用HTTPWATCH查看
$postfield="userName=test&year=2008&passWord=123456&Submit=%CC%E1%BD%BB";
//代理服务器
$proxy = '';
//请求
$str=curlrequest($url,$postfield,$proxy);
//输出结果
echo $str;
curl_close($ch);//Close<🎜>
<🎜>When CURL uses POST to submit XML data in PHP, be sure to define content-type as xml, otherwise the default is text/html! <🎜> <🎜><🎜>Example 2, post form data<🎜><🎜> <🎜>curl is a file transfer tool that uses URL syntax to work in command line mode. <🎜> PHP tutorial example: <🎜>
The code is as follows Copy code
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(); // Initialize CURL handle<🎜> if(!empty($proxy)){<🎜> curl_setopt ($ch, CURLOPT_PROXY, $proxy);//Set proxy server<🎜> }<🎜> curl_setopt($ch, CURLOPT_URL, $url); //Set the requested URL<🎜> //curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Display HTTP status code when enabled. The default behavior is to ignore HTTP information with a number less than or equal to 400<🎜> //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //When enabled, the "Location:" returned by the server will be placed in the header and returned to the server recursively <🎜> curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//Set to TRUE to convert the curl_exec() result into a string instead of directly outputting <🎜> curl_setopt($ch, CURLOPT_POST, 1);//Enable POST submission<🎜> curl_setopt($ch, CURLOPT_POSTFIELDS, $postfield); //Set the string for POST submission<🎜> //curl_setopt($ch, CURLOPT_PORT, 80); //Set port<🎜> curl_setopt($ch, CURLOPT_TIMEOUT, 25); // Timeout <🎜> curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);//HTTP request User-Agent: header <🎜> //curl_setopt($ch,CURLOPT_HEADER,1);//Set to TRUE to include header information in the output<🎜> //$fp = fopen("example_homepage.txt", "w");//Output file<🎜> //curl_setopt($ch, CURLOPT_FILE, $fp);//Set the location of the output file, the value is a resource type, the default is STDOUT (browser). <🎜> curl_setopt($ch,CURLOPT_HTTPHEADER,array(<🎜> 'Accept-Language: zh-cn',<🎜> 'Connection: Keep-Alive',<🎜> 'Cache-Control: no-cache'<🎜> ));//Set HTTP header information<🎜> $document = curl_exec($ch); //Execute predefined CURL<🎜> $info=curl_getinfo($ch); //Get the characteristics of the returned information<🎜> //print_r($info);<🎜> if($info[http_code]=="405"){<🎜> echo "bad proxy {$proxy}n"; //Proxy error<🎜> exit;<🎜> }<🎜> //curl_close($ch);<🎜> return $document;<🎜> }<🎜> //Request URL<🎜> $url="http://example.cn/getInfo.php";<🎜> //POST submits data and can be viewed via HTTPWATCH<🎜> $postfield="userName=test&year=2008&passWord=123456&Submit=%CC%E1%BD%BB";<🎜> //Proxy server<🎜> $proxy = '';<🎜> //Request<🎜> $str=curlrequest($url,$postfield,$proxy);<🎜> //Output results<🎜> echo $str;


Example 3, a simple login example using curl post


Simulate post login submission form problem

SOOPY class:
I wrote a program before to simulate a post to push some resources
At first, like everyone else, the first thing that came to mind was to use PHP’s own library CURL to simulate various Baidu and Google
I want to be lazy and see if there is a simpler class to implement it?
I discovered him anyway, he is a snoopy type. (Chinese name Shi Lu)

The code is as follows Copy code
代码如下 复制代码

//首先要引用这个类
include("/data/tools/pooy/Snoopy/Snoopy.class.php");
$snoopy = new Snoopy;
//$Parameters这个是要提交的数组
$Parameters["username"] = "user";
$Parameters["pass"] = "pass";
$file = "/test/test.jpg";
$serviceUrl = "http://www.你的地址/fileProcess.php";
$postfiles["image"] = $file; //$filename上传文件相对路径 例如"upload/taoav.jpg";image/jpg
$snoopy->_submit_type = "multipart/form-data"; //设定submit类型
$snoopy->submit($serviceUrl,$Parameters,$postfiles);

//First reference this class include("/data/tools/pooy/Snoopy/Snoopy.class.php"); $snoopy = new Snoopy; //$Parameters is the array to be submitted $Parameters["username"] = "user"; $Parameters["pass"] = "pass"; $file = "/test/test.jpg"; $serviceUrl = "http://www.your address/fileProcess.php"; $postfiles["image"] = $file; //$filename relative path to upload file For example "upload/taoav.jpg";image/jpg $snoopy->_submit_type = "multipart/form-data"; //Set the submit type $snoopy->submit($serviceUrl,$Parameters,$postfiles);


//$postforms, $postfiles are values ​​of the 2 types, where $postfiles is an array of uploaded files

The above example implements a POST form submission case. Due to the complex requirements, the function of snoopy could not meet my needs, so I started again
Go attack CURL.
CURL extension library:
This library is a relatively mature extension library with very powerful functions. Powerful enough to simulate any action of the browser.
The requirements are like this:
Log in to a website backend for the first time
Second interface page, and then start pushing a large amount of resources
(The specific logic here is abbreviated)
For the convenience of operation, I encapsulated several functions I need to simulate into a class. The short code is as follows:

The above is a perfect solution to this problem using CURL, which can effectively solve the cookie storage problem.
The code is as follows Copy code
/*
​​Simulation resource push class
2012-09-14 by POOY
*/
class TuisongPost{

//Use structure login authentication
Function TuisongPost(){

//Storage COOKIE file
global $cookie_jar;
                $this->cookie_jar = tempnam('./tmp','cookie');
             $url = "http://www.your address";

                $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); //Save cookie information

              $output1 = curl_exec($ch);

             curl_close($ch);

//echo $this->cookie_jar."n";
}  
/*Get group ID*/ 
Function getGid($groupname,$channel,$lanmu){

$url = "http://XXXX.com/creategroup";

//Format the data to be pushed
              $data = $this->getGidArr($groupname,$channel,$lanmu);

             $ch = curl_init();

              $Ref_url = "http://www.your address";

​​​​​ curl_setopt($ch, CURLOPT_URL, $url);

            curl_setopt($ch, CURLOPT_REFERER, $Ref_url);
             curl_setopt($ch, CURLOPT_POST, 1); //Submit data in post mode

               curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Return data instead of outputting it directly

               curl_setopt($ch, CURLOPT_HEADER, 0); // Set whether to display header information. 0 means not to display, 1 means to display. The default is 0

              curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_jar); //Send cookie file

             curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //Send POST data

              $output2 = curl_exec($ch);
//This return value is used as a basis for judgment
              return $output2;
             curl_close($ch);
//$this->unlink($this->cookie_jar);
}  

//Push data
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.your address";

​​​​​ curl_setopt($ch, CURLOPT_URL, $url);

            curl_setopt($ch, CURLOPT_REFERER, $Ref_url);
              curl_setopt($ch, CURLOPT_POST, 1); //Submit data in post mode

               curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Return data instead of outputting it directly

               curl_setopt($ch, CURLOPT_HEADER, 0); // Set whether to display header information. 0 means not to display, 1 means to display. The default is 0

              curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_jar); //Send cookie file

             curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //Send POST data

              $output2 = curl_exec($ch);               return $output2;
             curl_close($ch);
//$this->unlink($this->cookie_jar);
}  

/*Push data operation*/ 
Function sendMes($url,$img,$imgdesc,$groupid,$groupname,$channel,$lanmu)
{
//var_dump($this->cookie_jar);
                   //exit();
$url = "http://XXXX/add";

              $data = $this->getArr($img,$imgdesc,$groupid,$groupname,$channel,$lanmu);

             $ch = curl_init();

              $Ref_url = "http://www.your address";

​​​​​ curl_setopt($ch, CURLOPT_URL, $url);

            curl_setopt($ch, CURLOPT_REFERER, $Ref_url);
             curl_setopt($ch, CURLOPT_POST, 1); //Submit data in post mode

               curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Return data instead of outputting it directly

                curl_setopt($ch, CURLOPT_HEADER, 0); // Set whether to display header information. 0 means not to display, 1 means to display. The default is 0

              curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_jar); //Send cookie file

             curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //Send POST data

              $output2 = curl_exec($ch);
             curl_close($ch);
//$this->unlink($this->cookie_jar);
                                                                
Function getArr($img,$imgdesc,$groupid,$groupname,$channel,$lanmu)
{
             $post_data = array(
//Use the following writing method for windows, not applicable for linux
//"img"=>"@".$img.";type=image/jpeg",
"img"=>"@".$img,
"imgdesc"=>$imgdesc,
"groupid"=>$groupid,
"groupname"=>$groupname,
"channel"=>$channel,
"lanmu"=>$lanmu,
"cdate"=>date('Y-m-d') "Y-m-d" );
             return $post_data;
                                                                 //Format getGidArr
Function getGidArr($groupname,$channel,$lanmu)
                                                                              $post_data = array(
"groupname"=>$groupname,
"channel"=>$channel,
"lanmu"=>$lanmu,
"cdate"=>date('Y-m-d') "Y-m-d" );
             return $post_data;
                                                                 //Format sendPicArr
Function sendPicArr($note,$groupid,$groupindex,$img)
{
             $post_data = array(
"notes"=>$note,
"id"=>$groupid,
"index"=>$groupindex,
"cdate"=>date('Y-m-d'),
//Use the following writing method for windows, not applicable for linux
//"img"=>"@".$img.";type=image/jpeg",
"img"=>"@".$img "img" );
             return $post_data;
}  

//Clear cookie files
Function unlink($cookie_jar){
         unlink($cookie_jar);                                    }  
}

http://www.bkjia.com/PHPjc/632781.html

truehttp: //www.bkjia.com/PHPjc/632781.htmlTechArticleI have talked a lot about the php curl function to implement post submission data. Let me introduce a kind of submission to you. xml is a way to submit form data. Example 1 CURL uses POST to submit XML data Code...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn