Using Php CURLPOST to submit a form to log on to an instance _ PHP Tutorial

Source: Internet
Author: User
Use Php CURLPOST to submit a form to log on to the instance. I have mentioned a lot about the phpcurl function to implement post data submission. next I will introduce you to submit xml and form data. For example, 1CURL uses POST to submit XML data code. I have mentioned a lot about the php curl function to implement post to submit data. next I will introduce you to submit xml and form data.


Example 1

CURL uses POST to submit XML data

The code is as follows:

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

$ Ch = curl_init ();
$ Header [] = "Content-type: text/xml"; // Define content-type as xml
Curl_setopt ($ ch, CURLOPT_URL, $ url); // defines the form submission address
Curl_setopt ($ ch, CURLOPT_POST, 1); // defines the submission Type 1: POST; 0: GET
Curl_setopt ($ ch, CURLOPT_HEADER, 1); // defines whether to display Status Header 1: Display; 0: not Display
Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); // define the request type
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 0); // you can specify whether to directly output the returned stream.
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); // defines the submitted data, which is an XML file.
Curl_close ($ ch); // Close

When using POST to submit XML data in PHP, you must define content-type as xml. Otherwise, the default value is text/html!

Example 2: post form data

Curl is a file transfer tool that uses the URL syntax to work in the command line mode.
Php Tutorial example:

The code is as follows:
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 the CURL handle
If (! Empty ($ proxy )){
Curl_setopt ($ ch, CURLOPT_PROXY, $ proxy); // sets the proxy server
}
Curl_setopt ($ ch, CURLOPT_URL, $ url); // you can specify the request URL.
// Curl_setopt ($ ch, CURLOPT_FAILONERROR, 1); // The HTTP status code is displayed when it is enabled. the default behavior is to ignore the HTTP information whose number is less than or equal to 400.
// Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); // When enabled, the "Location:" returned by the server is put in the header and recursively returned to the server.
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); // you can specify a string for POST submission.
// Curl_setopt ($ ch, CURLOPT_PORT, 80); // Set the 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 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 value 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 the predefined CURL
$ Info = curl_getinfo ($ ch); // Obtain 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 ";
// Submit data in POST mode and can be viewed by HTTPWATCH.
$ Postfield = "userName = test & year = 2008 & passWord = 123456 & Submit = % CC % E1 % BD % BB ";
// Proxy server
$ Proxy = '';
// Request
$ Str = curlrequest ($ url, $ postfield, $ proxy );
// Output result
Echo $ str;


Example 3: a simple curl post login instance


Simulate post login and submit a form

SOOPY class:
I wrote a program to simulate post to push some resources.
At first, like everyone else, Baidu Google first came up with the library CURL that comes with PHP to simulate
I want to secretly and lazy to see if there are simpler classes for implementation?
I found out that it is the snoopy class. (Chinese name: RS)

The code is as follows:

// Reference this class first
Include ("/data/tools/pooy/Snoopy. class. php ");
$ Snoopy = new Snoopy;
// $ Parameters: 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: the relative path of the uploaded file, for example, "upload/taoav.jpg"; image/jpg
$ Snoopy-> _ submit_type = "multipart/form-data"; // you can specify the submit type.
$ Snoopy-> submit ($ serviceUrl, $ Parameters, $ postfiles );


// $ Postforms, $ postfiles is a value of type 2, where $ postfiles is an array of uploaded files

The above example shows a case of POST form submission. Due to the complex requirements, this snoopy function cannot meet my needs, so I started
Attack CURL.
CURL Extension Library:
This library is a mature extension library with powerful functions. Powerful enough to simulate any action of the browser.
The requirement is as follows:
First, log on to the background of a website.
Second interface page, and then start to push a large number of resources
(The specific logic here is reduced)
For ease of operation, I encapsulate several functions that I need to simulate into a class. The short code is as follows:

The code is as follows:
/*
Simulate resource push class
2012-09-14 by POOY
*/
Class TuisongPost {

// Construct login authentication
Function TuisongPost (){

// The file that stores the COOKIE
Global $ cookie_jar;
$ This-> cookie_jar = tempnam ('./tmp', 'cooker ');
$ 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 the 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); // disguise REFERER

Curl_setopt ($ ch, CURLOPT_POST, 1); // submit data in post mode

Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // return data instead of directly outputting

Curl_setopt ($ ch, CURLOPT_HEADER, 0); // you can specify whether to display the header information. 0 indicates that the header information is not displayed. 1 indicates that the default value is 0.

Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ this-> cookie_jar); // send the cookie file

Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); // send POST data

$ Output2 = curl_exec ($ ch); // send an HTTP request

// The returned value is used as the 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); // disguise REFERER

Curl_setopt ($ ch, CURLOPT_POST, 1); // submit data in post mode

Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // return data instead of directly outputting

Curl_setopt ($ ch, CURLOPT_HEADER, 0); // you can specify whether to display the header information. 0 indicates that the header information is not displayed. 1 indicates that the default value is 0.

Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ this-> cookie_jar); // send the cookie file

Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); // send POST data

$ Output2 = curl_exec ($ ch); // send an HTTP request
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); // disguise REFERER

Curl_setopt ($ ch, CURLOPT_POST, 1); // submit data in post mode

Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // return data instead of directly outputting

Curl_setopt ($ ch, CURLOPT_HEADER, 0); // you can specify whether to display the header information. 0 indicates that the header information is not displayed. 1 indicates that the default value is 0.

Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ this-> cookie_jar); // send the cookie file

Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); // send POST data

$ Output2 = curl_exec ($ ch); // send an HTTP request

Curl_close ($ ch );
// $ This-> unlink ($ this-> cookie_jar );
}

Function getArr ($ img, $ imgdesc, $ groupid, $ groupname, $ channel, $ lanmu)
{
$ Post_data = array (
// Use the following statement in windows. linux is not applicable.
// "Img" => "@". $ img. "; type = image/jpeg ",
"Img" => "@". $ img,
"Imgdesc" => $ imgdesc,
"Groupid" => $ groupid,
"Groupname" => $ groupname,
"Channel" => $ channel,
"Lanmu" => $ lanmu,
"Cdate" => date ('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 ')
);
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 statement in windows. linux is not applicable.
// "Img" => "@". $ img. "; type = image/jpeg ",
"Img" => "@". $ img
);
Return $ post_data;
}

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

The above uses CURL to solve this problem perfectly. it can effectively solve the cookie storage problem.

The curl function implements post data submission. next I will introduce a type of xml submission and a form of data submission. Example 1 CURL uses POST to submit XML data code...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.