PHP Curl mock POST request submission Data Example Summary

Source: Internet
Author: User
Tags curl file upload zip

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

Example 1

The code is as follows Copy Code

<?php
$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 PHP page Remote server:

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

Example 2

Using Curl simulate POST request to crawl ZIP code and address

Complete code:

The code is as follows 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 be written to the file $filename";
echo "n";
Exit
}


echo "successfully writes the $somecontent to the 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 ';

Simulate post requests to submit data or upload files.

.

The code is as follows Copy Code

http://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 enabled, a regular post request is sent, with the type: application/x-www-form-urlencoded, just like the form submission.
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-closes a curl session
curl_copy_handle-copies a curl handle and all its options
curl_errno-returns the last error number
curl_error-returns a The string that protects the most recent error of the current session
Curl_exec-executes a curl session
Curl_getinfo-Gets an information Curl connection resource handle
curl_init-initialize a curl session
Curl_multi_add_handle-adds a separate curl handle to the Curl batch session
Curl_multi_close-closes a set of curl handles
curl_multi_exec-runs the current curl A child of a handle
curl_multi_getcontent-returns the text stream of the obtained output
if Curlopt_returntransfer is set curl_multi_info_read- Gets the associated transport information for the currently resolved Curl
curl_multi_init-Returns a new Curl batch handle
curl_multi_remove_handle-removes a handle resource from the Curl batch handle resource
Curl_multi_select-waits for an active connection in all Curl Batches
curl_setopt_array-SET options for Curl Transport session bulk Setup
curl_setopt-set a Curl transport option
curl_version-Get Curl Version information

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.