


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 PHP sending get and post requests, 6 types of get
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:
[
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
