PHP Curl Analog POST request submit Data Sample Summary

Source: Internet
Author: User

In PHP to simulate POST request data submission we will use the Curl function, the following I would like to give you a few Curl analog post request submission data samples have the need for friends to participate in the test. Note: The curl function is not supported by default in PHP, assuming that we need to use the Curl function in PHP to simulate the POST request data submission we will use the Curl function, Let me give you a couple of Curl analog Post request submission Data sample A friend who has a need to participate in the test.

Note: The curl function is not supported by default in PHP, assuming that we need to use the Curl function to change the settings of your php.ini file and find Php_curl.dll to remove the previous ";" You're ready.

Example 1

Code such as the following Copy Code

<?php
$uri = "http://tanteng.duapp.com/test.php";
Array of parameters
$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:

<?php
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:

Code such as the following Copy Code

#!/usr/local/php/bin/php
<?

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]*?

<TR>[S]*?<TD class= "Maintext" >[ss]*?</td>[s]*?

</tr>/', $data, $matches);
if (! $handle = fopen ($filename, ' A + ')) {
echo "Cannot open file $filename";
echo "n";
Exit
}


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 ';

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

.

Code such as the following Copy Code

http://www.a.com/a.php

Send a 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 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 current session's recent error
Curl_exec-running 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 the Curl batch session
curl_multi_close-closing a set of curl handles
curl_multi_exec-to perform a child join of the current curl handle
Curl_multi_getcontent-assumes that the 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-Get Curl Version number information

PHP Curl Analog POST request submit Data Sample Summary

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.