Home >Backend Development >PHP Tutorial >A concise summary of the 6 methods of sending get and post requests in PHP, 6 types of get_PHP tutorial
Method 1: Use file_get_contents to get the content in get mode:
<?php $url='http://www.bkjia.com/'; $html = file_get_contents($url); echo $html; ?>
Method 2: Open the url with fopen and get the content with get method:
<?php $fp = fopen($url, ‘r'); stream_get_meta_data($fp); while(!feof($fp)) { $result .= fgets($fp, 1024); } echo “url body: $result”; fclose($fp); ?>
Method 3: Use the file_get_contents function to get the url in post mode
<?php $data = array (‘foo' => ‘bar'); $data = http_build_query($data); $opts = array ( ‘http' => array ( ‘method' => ‘POST', ‘header'=> “Content-type: application/x-www-form-urlencodedrn” . “Content-Length: ” . strlen($data) . “rn”, ‘content' => $data ) ); $context = stream_context_create($opts); $html = file_get_contents(‘http://localhost/e/admin/test.html', false, $context); echo $html; ?>
Method 4: Use the fsockopen function to open the url and obtain the complete data in get mode, including header and body. fsockopen requires the allow_url_fopen option in PHP.ini to be turned on
<?php function get_url ($url,$cookie=false) { $url = parse_url($url); $query = $url[path].”?”.$url[query]; echo “Query:”.$query; $fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30); if (!$fp) { return false; } else { $request = “GET $query HTTP/1.1rn”; $request .= “Host: $url[host]rn”; $request .= “Connection: Closern”; if($cookie) $request.=”Cookie: $cookien”; $request.=”rn”; fwrite($fp,$request); while(!@feof($fp)) { $result .= @fgets($fp, 1024); } fclose($fp); return $result; } } //获取url的html部分,去掉header function GetUrlHTML($url,$cookie=false) { $rowdata = get_url($url,$cookie); if($rowdata) { $body= stristr($rowdata,”rnrn”); $body=substr($body,4,strlen($body)); return $body; } return false; } ?>
Method 5: Use the fsockopen function to open the url and obtain the complete data in POST mode, including header and body
<?php function HTTP_Post($URL,$data,$cookie, $referrer=”") { // parsing the given URL $URL_Info=parse_url($URL); // Building referrer if($referrer==”") // if not given use this script as referrer $referrer=”111″; // making string from $data foreach($data as $key=>$value) $values[]=”$key=”.urlencode($value); $data_string=implode(“&”,$values); // Find out which port is needed – if not given use standard (=80) if(!isset($URL_Info["port"])) $URL_Info["port"]=80; // building POST-request: $request.=”POST “.$URL_Info["path"].” HTTP/1.1n”; $request.=”Host: “.$URL_Info["host"].”n”; $request.=”Referer: $referern”; $request.=”Content-type: application/x-www-form-urlencodedn”; $request.=”Content-length: “.strlen($data_string).”n”; $request.=”Connection: closen”; $request.=”Cookie: $cookien”; $request.=”n”; $request.=$data_string.”n”; $fp = fsockopen($URL_Info["host"],$URL_Info["port"]); fputs($fp, $request); while(!feof($fp)) { $result .= fgets($fp, 1024); } fclose($fp); return $result; } ?>
Method 6: Use the curl library. Before using the curl library, you may need to check whether the curl extension has been turned on in php.ini
<?php $ch = curl_init(); $timeout = 5; curl_setopt ($ch, CURLOPT_URL, ‘http://www.bkjia.com/'); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); curl_close($ch); echo $file_contents; ?>
Differences between Http Get/Post requests
1. HTTP request format:
b776c5fa5ead5b70b1d84639470497b2
4b6297737b15f96f7b66fea4e1ff5063
8d8faff3f29b3b7eb3728d6189cfbb3d
[d72bd414f7110404f2651f3795e8b3d5]
In an HTTP request, the first line must be a request line, which describes the request type, the resource to be accessed, and the HTTP version used. This is followed by a header section describing additional information to be used by the server. After the header is a blank line, after which any other data can be added (called the body).
1. Get is to obtain data from the server, and post is to transmit data to the server.
Get and post are just a way to transfer data. Get can also transfer data to the server. Their essence is to send requests and receive results. There are only differences in organizational format and data volume, which are introduced in the http protocol
2. Get adds the parameter data queue to the URL pointed to by the ACTION attribute of the submitted form. The value corresponds to each field in the form. In It can be seen in the URL. Post uses the HTTP post mechanism to place each field in the form and its content in the HTML HEADER and transmit it to the URL address pointed to by the ACTION attribute. Users cannot see this process.
Because get is designed to transmit small data, and it is best not to modify the server data, so the browser can usually see it in the address bar, but post is generally used to transmit big data, or relatively private data. So whether you can see it in the address bar or not is not stipulated by the agreement, but is stipulated by the browser.
3. For the get method, the server side uses Request.QueryString to obtain the value of the variable. For the post method, the server side uses Request.Form to obtain the submitted data.
I don’t understand. How to obtain variables has something to do with your server and has nothing to do with get or post. The server encapsulates these requests
4. The amount of data transmitted by get is small and cannot be larger than 2KB. The amount of data transmitted by post is relatively large and is generally unrestricted by default. But in theory, the maximum amount is 80KB in IIS4 and 100KB in IIS5.
There are basically no restrictions on post. I think everyone has uploaded files using the post method. It just needs to modify the type parameter in the form
5. The security of get is very low, but the security of post is high.
If there is no encryption, their security level is the same. Any listener can listen to all the data. If you don’t believe it, your next software to monitor network resources,
Get is sent to the server. A request to obtain data, and Post is a request to submit data to the server. In FORM (form), the Method defaults to "GET". In essence, GET and POST only have different sending mechanisms, and they do not take and send data one by one. !
Http defines different methods for interacting with the server. There are 4 most basic methods, namely GET, POST, PUT, and DELETE. The full name of URL is resource descriptor. We can think of it this way: a URL address is used to describe a resource on the network, and GET, POST, PUT, and DELETE in HTTP correspond to the search, modification, and addition of this resource. Delete 4 operations. At this point, everyone should have a general understanding. GET is generally used to obtain/query resource information, while POST is generally used to update resource information.
1. According to the HTTP specification, GET is used for information acquisition and should be safe and idempotent.
(1). The so-called safe means that the operation is used to obtain information rather than modify information. In other words, GET requests should generally not have side effects. That is to say, it only obtains resource information, just like a database query. It will not modify or add data, and will not affect the status of the resource.
* Note: The meaning of security here only refers to non-modified information.
(2). Idempotent means that multiple requests to the same URL should return the same result. Here I will explain the concept of idempotence again:
Idempotence (idempote...the rest of the full text>>
Change HTTP/1.1 to HTTP/1.0
$results=fgets($fp,1024);
$contents = substr($results,strpos($results,"\r\n\r\ n")+4); //Remove the header returned by the request
$header=substr($results,0,strpos($results,"\r\n\r\n")+1) ; //The corresponding header information
should be like this. In fact, this kind of PHP http class implemented with socket (simulating post or get)
You can refer to
www.wenlingnet.com/ archives/2009/12/05/67.html