Php:6 kinds of Get and post request sending methods

Source: Internet
Author: User

In the I94web blog, I tried to Chang and say two social comment boxes, and later abandoned the smooth speech, unsafe.

I need to crawl the number of comments from the article remotely and then deposit it into the local database, whether it's a word or a lot. For more said, the requested format is as follows:

Gets the number of comments, the argument is the article IDfunction getcommcount ($postid) {    $jsondata = file_get_contents ("HTTP/ api.duoshuo.com/threads/counts.json?short_name=i94web&threads= $postid ");    //Set True to return an array, not set or false to return an object    $resjson = Json_decode ($jsondata,return $resjson [' response '][$ postid][' comments ');}     

There are a number of ways to do this for remote requests. Today, the LZ has collected six kinds, for everyone's reference.

1, use file_get_contents to get the content:

<?php$url=' http://www.ido321.com/'; $html = file_get_contents ($url);  Echo $html;? > 

2, open the URL with fopen, get the way to obtain

' R '); Stream_get_meta_data ($FP); while (!feof ($fp)) {$result. = fgets ($fp, 1024);} "URL body: $result"; fclose ($fp); 

3. Use file_get_contents to post to get content:

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

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

functionGet_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) {Returnfalse;}else {$request =' GET $query http/1.1rn '; $request. = ' Connection:closern ' ; if ($cookie) $request. = ' Cookie: $cookien '; $request. =< Span style= "color: #006080;" > ' RN '; fwrite ($fp, $request); while ([email protected] ($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);  Rnrn '); $body =substr ($body, 4,strlen ($body)); return $body;} return false;}       

5. Open the URL with the Fsockopen function to get the complete data by post, including header and body

functionHttp_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 ($dataAs $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. =" Referer: $referern "; $request. =" Content-type:application/x-www-form-urlencodedn "; $request. = ' content-length: '. strlen ($data _string).  ' cookies: $cookien '; $request. = ' Port ']) ; Fputs ($fp, $request); while (!feof ($fp)) {$result. = fgets ($fp, 1024);} Fclose ($FP); return $result;}           

6. Using the Curl Library, before using the Curl Library, you may need to check to see if the php.ini has already turned on the Curl extension

$ch = Curl_init (); $timeout = 5;curl_setopt ($ch, Curlopt_url, ' http:echo $file _contents;

Php:6 kinds of Get and post request sending methods

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.