PHP simulation post behavior code summary_PHP tutorial

WBOY
Release: 2016-07-13 17:38:58
Original
877 people have browsed it

GET behavior is relatively simple, while POST is more complex. There are two methods to choose from: First: handwritten code. Second: Use the HttpClient php class library

First method:

PHP code
$flag = 0;
//Data to post                                        $argv = array(    
var1=>abc,  
var2=>How are you);  
//Construct the string to be posted
foreach ($argv as $key=>$value) {                                         if ($flag!=0) {
               $params .= "&";                                             $flag = 1;                                                                                                 $params.= $key."="; $params.= urlencode($value); $params.= $key."="; $params.= urlencode($value);        $flag = 1;                                                                                                   $length = strlen($params);                                                                                                                                                                                                                     $fp = fsockopen("127.0.0.1",80,$errno,$errstr,10) or exit($errstr."--->".$errno); //Construct the header of the post request
$header = "POST /mobile/try.php HTTP/1.1"; $header = "POST /mobile/try.php HTTP/1.1";
$header .= "Host:127.0.0.1"; $header .= "Host:127.0.0.1";
$header .= "Referer:/mobile/sendpost.php"; $header .= "Referer:/mobile/sendpost.php";
$header .= "Content-Type: application/x-www-form-urlencoded"; $header .= "Content-Type: application/x-www-form-urlencoded";
$header .= "Content-Length: ".$length.""; $header .= "Content-Length: ".$length."";
        $header .= "Connection: Close";                                //Add the string of the post
$header .= $params.""; $header .= $params."";
//Send post data                                                                                                                                                                $inheader = 1;                               While (!feof($fp)) {                                                                          $line = fgets($fp,1024); //Remove the header of the request packet and only display the return data of the page                                  If ($inheader && ($line == " " || $line == "")) {   
                        $inheader = 0;                                                                                                                               if ($inheader == 0) {                                                                           echo $line;                                                                                                                                                                            fclose($fp);                                        ?> 

The second method is: use httpclient class

PHP code
$pageContents = HttpClient::quickPost(http://example.com/someForm, array(    
name => Some Name, 
email => email@example.com
));

To use the httpclient class library, you can go to the official website to download the latest class library. The official address is: http://scripts.incutio.com/httpclient/index.php

Attach some other uses of php httpclient

Static method to obtain web page:

PHP code
$pageContents = HttpClient::quickGet(http://example.com/);  

Get method gets

PHP code
$client = new HttpClient(example.com); 
if (!$client->get(/)) {    
Die(An error occurred: .$client->getError()); Die(An error occurred: .$client->getError());
}       
$pageContents = $client->getContent();

Get method with debugging

PHP code
$client = new HttpClient(example.com);    
$client->setDebug(true);                             if (!$client->get(/)) {    
Die(An error occurred: .$client->getError()); Die(An error occurred: .$client->getError());
}       
$pageContents = $client->getContent();

Get method with automatic redirection

PHP code

$client = new HttpClient(www.amazon.com);   
$client->setDebug(true);                             if (!$client->get(/)) {          
Die(An error occurred: .$client->getError()); Die(An error occurred: .$client->getError());
}       
$pageContents = $client->getContent();

Check if the page exists

PHP code

$client = new HttpClient(example.com);    

$client->setDebug(true);                             if (!$client->get(/thispagedoesnotexist)) {     
Die(An error occurred: .$client->getError()); Die(An error occurred: .$client->getError());
}       
if ($client->getStatus() == 404) {     
echo Page does not exist!; }       
$pageContents = $client->getContent();

Fake client

PHP code

$client = new HttpClient(exa


http://www.bkjia.com/PHPjc/486434.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/486434.htmlTechArticleGET behavior is relatively simple, while POST is more complex. There are two methods to choose from: First: handwritten code. Second: Use HttpClient php class library First method: PHP code? PHP...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!