Use PHP's Curl post to submit form login Examples _php Tutorial

Source: Internet
Author: User
There are a lot of previous talk about PHP curl function to implement post submission data, let me introduce a kind of commit XML 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";//define Content-type as XML
curl_setopt ($ch, Curlopt_url, $url); Define form submission Address
curl_setopt ($ch, Curlopt_post, 1); Define the commit type 1:post; 0:get
curl_setopt ($ch, Curlopt_header, 1); Defines whether to display status Header 1: display; 0: Do not display
curl_setopt ($ch, Curlopt_httpheader, $header);//define Request type
curl_setopt ($ch, Curlopt_returntransfer, 0);//defines whether direct output returns a stream
curl_setopt ($ch, Curlopt_postfields, $data); Define the submitted data, here is the XML file
Curl_close ($ch);//Close

When using post to commit 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 works with URL syntax in the command line mode.
Examples of PHP Tutorials:

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 the curl handle
if (!empty ($proxy)) {
curl_setopt ($ch, Curlopt_proxy, $proxy);//Setting up a proxy server
}
curl_setopt ($ch, Curlopt_url, $url); Set the requested URL
curl_setopt ($ch, Curlopt_failonerror, 1); Display HTTP status code when enabled, default behavior is to ignore HTTP messages with numbers less than or equal to 400
curl_setopt ($ch, curlopt_followlocation, 1);//When enabled, returns the "location:" returned by the server server to the server recursively in the header
curl_setopt ($ch, curlopt_returntransfer,1);//set to True to convert curl_exec () results to strings instead of direct output
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); Setting up ports
curl_setopt ($ch, Curlopt_timeout, 25); Timeout period
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);//sets 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); Perform predefined curl
$info =curl_getinfo ($ch); Get the characteristics of the return information
Print_r ($info);
if ($info [http_code]== "405") {
echo "Bad proxy {$proxy}n"; Agent Error
Exit
}
Curl_close ($ch);
return $document;
}
Request URL
$url = "http://example.cn/getInfo.php";
Post submission data, available HttpWatch view
$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 instance using Curl Post


Analog Post Login Submit form issue

Soopy class:
Previously wrote a program that simulates post to push some resources
At first, like everyone else, all kinds of Baidu Google, the first thing to think about is to use PHP's own library curl this to simulate
I want to steal lazy to see if there are simpler classes to achieve it?
Or I found out, he is the Snoopy class. (Chinese name Shlubb)

copy code

//first to refer to this class
include ("/data/tools/pooy/ Snoopy/snoopy.class.php ");
$snoopy = new Snoopy;
//$Parameters This is the array to commit
$Parameters ["username"] = "user";
$Parameters ["pass"] = "pass";
$file = "/test/test.jpg";
$serviceUrl = "http://www. your address/fileprocess.php";
$postfiles ["image"] = $file;//$filename upload file relative path such as "upload/taoav.jpg"; image/jpg
$snoopy->_submit_type = " Multipart/form-data "; Set the type of Submit
$snoopy->submit ($SERVICEURL, $Parameters, $postfiles);


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

The example above is the implementation of a post form submission case. Because of the complexity of the requirements, this Snoopy function is not satisfied with my needs, and then began
To attack Curl.
Curl Extension Library:
This library is a relatively mature extension of the library, the function is very powerful. Powerful to be able to simulate any action of the browser.
The requirement is this:
First landing a website backstage
Second interface page, and then start pushing a lot of resources
(The specific logic in this is abbreviated)
For ease of operation, I encapsulated several functions I needed to simulate into a class, with the short code as follows:

The code is as follows Copy Code
/*
Simulation Resource Push class
2012-09-14 by Pooy
*/
Class tuisongpost{

Using Construction login Authentication
function Tuisongpost () {

Files that store cookies
Global $cookie _jar;
$this->cookie_jar = Tempnam ('./tmp ', ' Cookie ');
$url = "http://www. your address";

$post _data = Array ("username" = "admin", "password" and "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

$output 1 = 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); Camouflage Referer

curl_setopt ($ch, Curlopt_post, 1); Post mode submit data

curl_setopt ($ch, Curlopt_returntransfer, 1); Return data instead of direct output

curl_setopt ($ch, Curlopt_header, 0); Set whether to show header information 0 is not displayed, 1 is display default is 0

curl_setopt ($ch, Curlopt_cookiefile, $this->cookie_jar); Send Cookie File

curl_setopt ($ch, Curlopt_postfields, $data); Send post data

$output 2 = curl_exec ($ch); Send HTTP request

This return value is used as a basis for judgment.
return $output 2;
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); Camouflage Referer

curl_setopt ($ch, Curlopt_post, 1); Post mode submit data

curl_setopt ($ch, Curlopt_returntransfer, 1); Return data instead of direct output

curl_setopt ($ch, Curlopt_header, 0); Set whether to show header information 0 is not displayed, 1 is display default is 0

curl_setopt ($ch, Curlopt_cookiefile, $this->cookie_jar); Send Cookie File

curl_setopt ($ch, Curlopt_postfields, $data); Send post data

$output 2 = curl_exec ($ch); Send HTTP request
return $output 2;
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); Camouflage Referer

curl_setopt ($ch, Curlopt_post, 1); Post mode submit data

curl_setopt ($ch, Curlopt_returntransfer, 1); Return data instead of direct output

curl_setopt ($ch, Curlopt_header, 0); Set whether to show header information 0 is not displayed, 1 is display default is 0

curl_setopt ($ch, Curlopt_cookiefile, $this->cookie_jar); Send Cookie File

curl_setopt ($ch, Curlopt_postfields, $data); Send post data

$output 2 = curl_exec ($ch); Send HTTP request

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

function Getarr ($img, $imgdesc, $groupid, $groupname, $channel, $LANMU)
{
$post _data = Array (
Windows uses the following notation, Linux does not apply
"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;
}
Formatting Getgidarr
function Getgidarr ($groupname, $channel, $LANMU)
{
$post _data = Array (
"GroupName" = $groupname,
"Channel" = $channel,
"Lanmu" = $lanmu,
"CDate" =>date (' y-m-d ')
);
return $post _data;
}
Formatting Sendpicarr
function Sendpicarr ($note, $groupid, $groupindex, $img)
{
$post _data = Array (
"Notes" = $note,
"id" = $groupid,
"Index" = $groupindex,
"CDate" =>date (' y-m-d '),
Windows uses the following notation, Linux does not apply
"img" = "@". $img. "; Type=image/jpeg ",
"img" = "@". $img
);
return $post _data;
}

Clean up Cookie Files
Function unlink ($cookie _jar) {
Unlink ($cookie _jar);
}
}

The above is using curl to solve this problem perfectly, he can effectively solve the problem of cookie storage.

http://www.bkjia.com/PHPjc/632781.html www.bkjia.com true http://www.bkjia.com/PHPjc/632781.html techarticle There are a lot of previous talk about PHP curl function to implement post submission data, let me introduce a kind of commit XML to submit form data. 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.