


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 is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
