PHP 6 ways to send get, post requests concise summary, Get6 kind of _php tutorial

Source: Internet
Author: User
Tags html header http post

PHP 6 ways to send a GET, POST request concise summary, Get6 species


Method 1: Use file_get_contents to get the content in the 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 in get:

<?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 by post

<?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: Open the URL with the Fsockopen function, get the complete data in Get mode, including header and Body,fsockopen need php.ini allow_url_fopen option to open

<?phpfunction 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,); if (! $fp) { return false;} else {$request = "GET $query http/1.1rn"; $request. = "Host: $url [Host]rn"; $request. = "Connection:closern"; if ($cookie) $r Equest.= "Cookie:  $cookien"; $request. = "RN"; fwrite ($fp, $request); while (! @feof ($fp)) {$result. = @fgets ($FP, 1024 );} Fclose ($FP); return $result;}} Gets the HTML portion of the URL, removing headerfunction geturlhtml ($url, $cookie =false) {$rowdata = Get_url ($url, $cookie), or 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, post to get the full data, including header and body

<?phpfunction http_post ($URL, $data, $cookie, $referrer = "") {//parsing the given url$url_info=  Parse_url ($URL);//Building Referrerif ($referrer = = "")//if not given with this script as referrer$referrer= "111″;//making String from $dataforeach ($data as $key = = $value) $values []= "$key =". UrlEncode ($value); $data _string=implode ("&" , $values);//Find out which port was 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: Using the Curl Library, before using the Curl Library, you may need to see if php.ini has the curl extension turned on

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

10. For the post mode of HTTP, which part of the HTTP is the user-submitted form data? b a) in the head ......... .....

HTTP Get/post Request Differences
1. HTTP request Format:







[ ]

In an HTTP request, the first line must be a request line, which describes the type of request, the resources to be accessed, and the HTTP version used. This is followed by a header section that describes the additional information that the server will use. After the header is a blank line, then you can add any additional data [called the body].

1. Get is the data that is fetched from the server and post is the data sent to the server.
Get and post are just a way of passing data, and get can also send data to the server, and they are essentially sending requests and receiving results. Only the organization format and the amount of data there is a difference, the HTTP protocol inside the introduction
2. Get is the URL where the parameter data queue is added to the Action property of the submission form, and the value corresponds to the field one by one in the form, which is visible in the URL. Post is the HTTP post mechanism that places the fields within the form with their contents in the HTML header, along with the URL address referred to by the Action property. The user does not see the process.
Because get is designed to transfer small data, and it is best not to modify the server's data, so the browser is generally seen in the address bar, but post is generally used to pass big data, or more private data, so in the address bar can not see, can see is not the protocol, is the browser rules.
3. For Get mode, the server side uses Request.QueryString to get the value of the variable, and for post, the server side uses Request.Form to obtain the submitted data.
Do not understand, how to get the variable is related to your server, and get or post independent, the server has been encapsulated in these requests
4. Get transmits a small amount of data, cannot be greater than 2KB. Post transmits a large amount of data, which is generally not restricted by default. In theory, however, the maximum amount of IIS4 is 100KB in 80KB,IIS5.
Post basically no limit, I think we all uploaded files, are used post mode. Just to modify the type parameter in the form
5. Get security is very low, post security is high.
If there is no encryption, they are the same level of security, any listener can listen to all the data, do not believe your own next network resources to monitor the software,

Get is a request to send data to the server, and post is a request to submit data to the server, in form (form), the method defaults to "get", in essence, GET and post just send mechanism is different, not one to take a hair!
HTTP defines different ways to interact with the server, with 4 basic methods, namely get,post,put,delete. URL full name is a resource descriptor, we can think: a URL address, which is used to describe a network of resources, and HTTP get,post,put,delete corresponding to this resource, change, increase, delete 4 operations. Here, you should have a general understanding, get is generally used to get/query resource information, and post is generally used to update resource information.
1. According to the HTTP specification, get is used for information acquisition and should be secure and idempotent.

(1). So-called security means that the operation is used to obtain information rather than modify information. In other words, GET requests generally should not have side effects. That is, it simply gets the resource information, just like a database query, without modification, adding data without affecting the state of the resource.

* Note: The meaning of security here is simply non-modification information.

(2). Idempotent means that multiple requests to the same URL should return the same result. Here I'll explain the concept of idempotent:

Idempotent (Idempote ... Remaining full text >>

PHP for how 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

This should be the case, in fact, the socket implementation of the PHP HTTP class (analog post or get)
can refer to the following
Www.wenlingnet.com/archives/2009/12/05/67.html

http://www.bkjia.com/PHPjc/840764.html www.bkjia.com true http://www.bkjia.com/PHPjc/840764.html techarticle PHP 6 ways to send get, post requests concise summary, Get6 Method 1: Use file_get_contents to get the content: Php$url= ' http://www.bkjia.com/'; $html = File_ Get_contents ($ur ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.