PHP methods for simulating http requests and php requests
This example describes how to simulate an http request in PHP. We will share this with you for your reference. The details are as follows:
Method 1: Use php socket programming to directly send data to the interface to simulate post operations.
Create two files: post. php and getpost. php.
The post. php content is as follows:
<? Php $ flag = 0; $ params = ''; $ errno =''; $ errstr = ''; // The data to post $ argv = array ('var1' => 'abc', 'var2' => 'How are you, my friend ?? '); // Construct the string foreach ($ argv as $ key => $ value) {if ($ flag! = 0) {$ params. = "&"; $ flag = 1 ;}$ params. = $ key. "="; $ params. = urlencode ($ value); $ flag = 1 ;}$ length = strlen ($ params); // create a socket connection $ fp = fsockopen ("localhost", 81, $ errno, $ errstr, 10) or exit ($ errstr. "---> ". $ errno); // construct the header of the post request $ header = "POST/flandy/getpost. php HTTP/1.1 \ r \ n "; $ header. = "Host: 127.0.0.1 \ r \ n"; $ header. = "Referer:/flandy/post. php \ r \ n "; $ header. = "Content-Type: application/x-ww W-form-urlencoded \ r \ n "; $ header. = "Content-Length :". $ length. "\ r \ n"; $ header. = "Connection: Close \ r \ n"; // Add the post string $ header. = $ params. "\ r \ n"; // send post data fputs ($ fp, $ header); $ inheader = 1; while (! Feof ($ fp) {$ line = fgets ($ fp, 1024 ); // only the returned data on the page is displayed when the request packet header is removed. if ($ inheader & ($ line = "\ n" | $ line = "\ r \ n" )) {$ inheader = 0;} if ($ inheader = 0) {echo $ line ;}} fclose ($ fp);?>
The content of getpost. php is as follows:
<?phpecho "this is the data posted";echo "<pre>";print_r($_REQUEST);echo "</pre>";?>
Result output:
this is the data postedArray( [var1] => abc [var2] => how are you , my friend??)
The above code has passed the test on port 81 of the local machine.
Method 2: Use PHP curl extension or HttpClient. class. php classThese two are very similar. The following is a simple list of curl implementation code.
Two files: post2.php and getpost2.php
Post2.php content is as follows:
<? Php $ response code = 'nde005 '; $ website = 'www .baidu.com'; $ amt = 1; $ pwd = 123456; $ ch = curl_init (); $ curl_url = "http: // localhost: 81/flandy/getpost2.php? Web = ". $ website. "& pwd = ". $ pwd. "& action = check & Consumer id = ". $ response code. "& amt = ". $ amt; curl_setopt ($ ch, CURLOPT_URL, $ curl_url); curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // no direct output, return to the variable $ curl_result = curl_exec ($ ch); $ result = explode (',', $ curl_result); curl_close ($ ch); print_r ($ result);?>
The getpost2.php content is as follows:
<?phpecho "returndata<br>";echo "<pre>";print_r($_REQUEST);echo "</pre>";?>
Result output:
Array ( [0] => returndataArray( [web] => 'wwwbaiducom' [pwd] => 123456 [action] => check [pseid] => 'NDE005' [amt] => 1))
Method 3: Use the third-party class library HttpClient
Here you can download: http://scripts.incutio.com/httpclient/
Or click hereDownload from this site.
<?phprequire_once 'HttpClient.class.php';$params = array('web' => 'www.baidu.com','pwd' => '123456','action' => 'check','pseid' => 'NDE005','amt' => 1);$pageContents = HttpClient::quickPost('http://localhost:81/flandy/getpost3.php', $params);$result = explode(',', $pageContents);print_r($result);?>