Phpcurl simulates post request to submit data example summary _ PHP Tutorial

Source: Internet
Author: User
Phpcurl simulates post request to submit data example summary. In php, we will use the curl function to simulate post request data submission. below I will give you a few examples of curl simulating post request data submission. For more information, see. Note: We will use the curl function to simulate post request data submission in php. below I will give you a few examples of curl simulating post request data submission. if you have any need, refer.

Note: the curl function is not supported by default in php. if you need to use the curl function, you need to modify your php. set the INI file and find php_curl.dll to remove the previous ";".

Example 1

The code is as follows:

$ 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:

If (isset ($ _ POST ['name']) {
If (! Empty ($ _ POST ['name']) {
Echo 'hello, ', $ _ POST ['name'].'! ';
}
}
?>

Example 2

Use CURL to simulate POST request grabbing zip code and address

Complete code:







If (fwrite ($ handle, $ matches [0] [1]) === FALSE ){
Echo "cannot be written to file $ filename ";
Echo "n ";
Exit;
}


Echo "successfully writes $ 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 ';

[S] *? [S] *? /', $ Data, $ matches );If (! $ Handle = fopen ($ filename, 'A + ')){Echo "the file $ filename cannot be opened ";Echo "n ";Exit;}
The code is as follows:

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


$ Cookie_jar = tempnam ('/tmp', 'cooke ');


$ 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' => 'goopsearch ',
'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 );
Currochelle 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] *?

Simulate a POST request to submit data or upload files.

.

The code is as follows:

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' => '123 ',
'File' => '@ d: usrwwwtranslatedocumentReadme.txt'
);
Curl_setopt ($ ch, CURLOPT_HEADER, false );
// When enabled, a conventional POST request will be sent. the type is application/x-www-form-urlencoded, just like 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-close a cURL session
■ Curl_copy_handle-copy a cURL handle and all its options
■ Curl_errno-returns the last error code
■ Curl_error-returns a string that protects the last error of the current session.
■ Curl_exec-execute a cURL session
■ Curl_getinfo-obtains the information of a cURL connection resource handle.
■ Curl_init-initialize a cURL session
■ Curl_multi_add_handle-add a separate curl handle to the curl batch processing session
■ Curl_multi_close-close a group of cURL handles
■ Curl_multi_exec-subconnection for running the current cURL handle
■ Curl_multi_getcontent-if CURLOPT_RETURNTRANSFER is set, the obtained output text stream is returned.
■ Curl_multi_info_read-obtains the transmission information of the currently resolved cURL.
■ Curl_multi_init-return a new cURL batch handle
■ Curl_multi_remove_handle-removes a handle resource from the curl batch processing handle.
■ Curl_multi_select-wait for all active connections in the cURL batch processing
■ Curl_setopt_array-set the option for cURL transmission sessions in batches
■ Curl_setopt-set a cURL transfer option
■ Curl_version-obtain cURL version information


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