6 Methods for sending get and post requests by php are summarized in a concise manner, and the get6 _ PHP Tutorial

Source: Internet
Author: User
Tags html header
Php provides six methods for sending get and post requests, and get6 methods. Php sends get and post requests in a concise summary. get6 methods 1: Use file_get_contents to get the content in the get method: php $ urlwww.bkjia.com; $ htmlfile_get_contents (six methods for sending get and post requests by $ ur php are concise summary, and six methods are get6

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: use fopen to open the url and get the content:

<?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 obtain 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 get the complete data, including the header and body. fsockopen must be enabled using the allow_url_fopen option in PHP. ini.

<? 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, 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 ;}// get the html part of the url, remove headerfunction 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 the 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 use 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 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 php. ini has enabled the curl extension.

<?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 http post method, which part of the form data submitted by the user is located in HTTP? B a) starting .....................

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







[ ]

In an HTTP request, the first line must be a request line to describe the request type, resources to be accessed, and the HTTP version used. Next is a header section, which describes additional information to be used by the server. After the header is a blank line, you can add any other data [called the body].

1. get is to get data from the server, and post is to send data to the server.
Get and post are just a way to pass data. get can also transmit data to the server. they are essentially sending requests and receiving results. There is only a difference between the organization format and the data size, as described in the http protocol.
2. get is to add the parameter data queue to the URL referred to by the ACTION attribute of the submission form. the values correspond to each field in the form one by one and can be seen in the URL. Post uses the HTTP post mechanism to place fields in the form and their content in the html header and send them to the URL address referred to by the ACTION attribute. You cannot see this process.
Because get is designed to transmit small data, and it is best not to modify the server data, browsers generally can see it in the address bar, but post is generally used to transmit big data, or relatively private data, so you cannot see it in the address bar. can you see that it is not the protocol, but the browser.
3. for the get method, the server uses Request. QueryString to obtain the value of the variable. for the post method, the server uses Request. Form to obtain the submitted data.
I don't understand. how to obtain the variables is related to your server and has nothing to do with get or post. the server encapsulates these requests.
4. the data volume transmitted by get is small and cannot exceed 2 kB. The amount of data transmitted by post is large, which is generally not restricted by default. Theoretically, the maximum size of IIS4 is 80 kB, and that of IIS5 is 100KB.
There are basically no restrictions on post. I think all the files uploaded are post-based. You only need to modify the type parameter in form.
5. Low get security and high post security.
If there is no encryption, their security levels are the same. any listener can listen to all the data. do not believe your next software to listen to network resources,

Get is a type of request to request data from the server, while Post is a type of request to submit data to the server. in FORM, the default Method is "GET". in essence, GET and POST are only different sending mechanisms, not a single sending!
Http defines different methods for interaction with the server. There are four basic methods: GET, POST, PUT, and DELETE. The full name of a URL is a resource descriptor. we can think that a URL address is used to describe resources on a network, while GET, POST, PUT, DELETE corresponds to the query, modify, add, and DELETE operations on this resource. Here, you should have a rough understanding. GET is generally used to obtain/query resource information, while POST is generally used to update resource information.
1. according to HTTP specifications, GET is used for information retrieval, and should be secure and idempotent.

(1). The so-called security means that the operation is used to obtain information rather than modify information. In other words, GET requests generally do not have side effects. That is to say, it only obtains the resource information, just like the database query. it does not modify, add data, and does not affect the resource status.

* Note: security only indicates that the information is not modified.

(2) idempotence means that multiple requests to the same URL should return the same result. Here I will explain the concept of idempotence:

Idempotence (idempote ...... remaining 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") + 4); // remove the header returned by the request

$ Header = substr ($ results, 0, strpos ($ results, "\ r \ n") + 1); // corresponding header information

This should be the case. In fact, this PHP http class implemented using socket (simulating post or get)
Refer
Www.wenlingnet.com/archives/2009/12/05/67.html

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