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

A concise summary of the 6 methods of sending get and post requests in PHP, 6 types of get_PHP tutorial

WBOY
Release: 2016-07-13 10:23:12
Original
1388 people have browsed it

A concise summary of the 6 methods of PHP sending get and post requests, 6 types of get

Method 1: Use file_get_contents to get the content in get mode:

<&#63;php
$url='http://www.bkjia.com/';
$html = file_get_contents($url);
echo $html;
&#63;>
Copy after login

Method 2: Open the url with fopen and get the content with get method:

<&#63;php
$fp = fopen($url, ‘r');
stream_get_meta_data($fp);
while(!feof($fp)) {
$result .= fgets($fp, 1024);
}
echo “url body: $result”;
fclose($fp);
&#63;>
Copy after login

Method 3: Use the file_get_contents function to get the url in post mode

<&#63;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;
&#63;>
Copy after login

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

<&#63;php
function get_url ($url,$cookie=false)
{
$url = parse_url($url);
$query = $url[path].”&#63;”.$url[query];
echo “Query:”.$query;
$fp = fsockopen( $url[host], $url[port]&#63;$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;
}
&#63;>
Copy after login

Method 5: Use the fsockopen function to open the url and obtain the complete data in POST mode, including header and body

<&#63;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;
}

&#63;>
Copy after login

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

<&#63;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;
&#63;>
Copy after login

10. For the HTTP POST method, in which part of HTTP is the form data submitted by the user located? b a) Head ………………

Differences between Http Get/Post requests
1. HTTP request format:







[]

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

php for how to get post http

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/840764.htmlTechArticleA concise summary of the 6 methods of php sending get and post requests, get 6 methods 1: Use file_get_contents to obtain in get mode Content: php$url='http://www.bkjia.com/';$html = file_get_contents($ur...
Related labels:
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