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 | ||||||||
$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
|
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 | ||||
|
//$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 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