PHP Curl Simulation POST Request submission Data Example Summary _php tutorial

Source: Internet
Author: User
In PHP to simulate POST request data submission we will use the CURL function, below I would like to give you a few Curl analog Post request submission data examples of friends who need to refer to.

Note: The curl function is not supported by default in PHP, if you need to use the Curl function we need to change the settings of your php.ini file to find Php_curl.dll to remove the previous ";" It's all right.

Example 1

The code is as follows Copy Code

$uri = "http://tanteng.duapp.com/test.php";
Parameter array
$data = Array (
' Name ' = ' Tanteng '
' Password ' = ' password '
);

$ch = Curl_init ();
Print_r ($ch);
curl_setopt ($ch, Curlopt_url, $uri);
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_header, 0);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_postfields, $data);
$return = curl_exec ($ch);
Curl_close ($ch);

Print_r ($return);

Accept the PHP page remote server:

if (isset ($_post[' name ')) {
if (!empty ($_post[' name ')) {
echo ' Hello, ', $_post[' name ']. '! ';
}
}
?>

Example 2

Use curl to simulate post request crawl ZIP code and address

Full code:







if (fwrite ($handle, $matches [0][1]) = = = = FALSE) {
echo "Cannot write to file $filename";
echo "n";
Exit
}


echo "successfully wrote $somecontent to file $filename";
echo "n";


Fclose ($handle);
Curl_close ($ch);
}


Class runtime
{
var $StartTime = 0;
var $StopTime = 0;
function Get_microtime ()
{
List ($usec, $sec) =explode (", Microtime ()); return (float) $usec + (float) $sec);
}
function Start ()
{
$this->starttime= $this->get_microtime ();
}
function Stop () {
$this->stoptime= $this->get_microtime ();
}
function spent ()
{
Return ($this->stoptime-$this->starttime);
}
}


$runtime->stop ();


$con = ' processed in '. $runtime->spent (). ' Seconds ';
echo ' processed in '. $runtime->spent (). ' Seconds ';

[s]*? [s]*? /', $data, $matches);if (! $handle = fopen ($filename, ' A + ')) {echo "Cannot open file $filename";echo "n";Exit}
The code is as follows Copy Code

#!/usr/local/php/bin/php
$runtime = new runtime ();
$runtime->start ();


$cookie _jar = Tempnam ('/tmp ', ' Cookie ');


$filename = $argv [1];
$start _num= $argv [2];
$end _num = $argv [3];


for ($i = $start _num; $i < $end _num; $i + +) {
$zip = sprintf (' 6s ', $i);


$fields _post = Array (
' Postcode ' = $zip,
' Querykind ' = 2,
' Reqcode ' = ' gotosearch ',
' Search_button.x ' =>37,
' Search_button.y ' =>12
);


$fields _string = http_build_query ($fields _post, ' & ');
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, "url?reqcode=gotosearch&querykind=2&postcode=". $zip);
curl_setopt ($ch, Curlopt_header, true);
curl_setopt ($ch, Curlopt_returntransfer, true);
curl_setopt ($ch, Curlopt_post, true);
curl_setopt ($ch, Curlopt_connecttimeout, 120);
curl_setopt ($ch, Curlopt_referer, $refer);
curl_setopt ($ch, Curlopt_httpheader, $headers _login);
curl_setopt ($ch, Curlopt_cookiefile, $cookie _jar);
curl_setopt ($ch, Curlopt_cookiejar, $cookie _jar);
curl_setopt ($ch, curlopt_useragent, $_server[' http_user_agent ');
curl_setopt ($ch, Curlopt_post, 1); Send a regular POST request
curl_setopt ($ch, Curlopt_postfields, $fields _string);


$data = curl_exec ($ch);
Preg_match_all ('/id= ' table1 ' >[s]*?

[ss]*?

Impersonate a POST request to submit data or upload a file.

.

The code is as follows Copy Code

Htt p://www.a.com/a.php

Send POST request

Function execupload () {


$file = '/doucment/readme.txt ';
$ch = Curl_init ();
$post _data = Array (
' loginfield ' = ' username ',
' username ' = ' ybb ',
' password ' = ' 123456 ',
' File ' = ' @d:usrwwwtranslatedocumentreadme.txt '
);
curl_setopt ($ch, Curlopt_header, false); When
//is enabled, a regular post request is sent with the type: application/x-www-form-urlencoded, just as the form was submitted.
curl_setopt ($ch, Curlopt_post, true);
curl_setopt ($ch, curlopt_binarytransfer,true);
curl_setopt ($ch, Curlopt_postfields, $post _data);
curl_setopt ($ch, Curlopt_url, ' http://www.b.com/handleUpload.php ');
$info = curl_exec ($ch);
Curl_close ($ch);

Print_r ($info);

}

2.http://www.b.com/handleupload.php

Function handleupload () {
Print_r ($_post);
Echo ' ===file upload info: ';
Print_r ($_files);
}

CURL function

curl_close-closing a Curl session
curl_copy_handle-copy a curl handle and all of its options
curl_errno-returns the last error number
curl_error-returns a string that protects the most recent error of the current session
Curl_exec-performing a Curl session
Curl_getinfo-gets the information for a Curl connection resource handle
curl_init-initialization of a curl session
curl_multi_add_handle-Adding a separate curl handle to a curl batch session
curl_multi_close-closing a set of curl handles
curl_multi_exec-A child connection that runs the current curl handle
curl_multi_getcontent-if Curlopt_returntransfer is set, returns the text stream of the obtained output
Curl_multi_info_read-Gets the related transfer information for the currently resolved curl
curl_multi_init-returns a new Curl batch handle
curl_multi_remove_handle-removing a handle resource from the Curl batch handle resource
curl_multi_select-waiting for active connections in all Curl batches
curl_setopt_array-Bulk setup options for Curl transfer sessions
curl_setopt-Setting a Curl Transfer option
curl_version-Getting Curl Version information


http://www.bkjia.com/PHPjc/444612.html www.bkjia.com true http://www.bkjia.com/PHPjc/444612.html techarticle in PHP to simulate POST request data submission we will use the CURL function, below I would like to give you a few Curl analog Post request submission data examples of friends who need to refer to. Note: ...

  • 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.