Home > Article > Backend Development > PHP curl simulates post request to submit data example summary_PHP tutorial
To simulate post request data submission in PHP, we will use the curl function. Let me give you a few examples of curl simulating post request data submission. Friends in need can refer to it.
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. Find php_curl.dll and remove the ";" in front of it
Example 1
Copy code | |||||
// 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
Use CURL to simulate POST request to capture zip code and address
Full code:
The code is as follows | Copy code | |||||
$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, zip = sprintf('6s', 2, 'reqCode' => 'gotoSearch', 'search_button. 🎜> $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]*? | [s]*?
Simulate POST request to submit data or upload files.
.
The code is as follows
|
Copy code Send POST request |
2.http://www.b.com/handleUpload.php
■curl_close — Close a cURL session ■curl_copy_handle — Copy a cURL handle and all its options