Summary of several methods for sending GET and POST requests in php

Source: Internet
Author: User
Tags comments curl strlen

Whether talking or speaking, I need to capture the comments of the article remotely and store them in the local database. For more information, the request format is as follows:

The code is as follows: Copy code

// Gets the number of comments. The parameter is the article ID.
Function getCommCount ($ postid)
{
$ Jsondata = file_get_contents ("http://api.duoshuo.com/threads/counts.json? Short_name = i94web & threads = $ postid ");
// Set true to return an array. If not set or false, the object is returned.
$ Resjson = json_decode ($ jsondata, true );
Return $ resjson ['response'] [$ postid] ['comments'];
}
 

There are many methods for remote requests. Today, LZ has collected six types for your reference.

1. Use file_get_contents to get the content in get mode:

The code is as follows: Copy code
<? Php
$ Url = 'http: // www.111cn.net /';
$ Html = file_get_contents ($ url );
Echo $ html;
?>

 

2. Open the url with fopen and get it with get

The code is as follows: Copy code
$ Fp = fopen ($ url, 'r ');
Stream_get_meta_data ($ fp );
While (! Feof ($ fp )){
$ Result. = fgets ($ fp, 1024 );
}
Echo "url body: $ result ";
Fclose ($ fp );

  

3. Use file_get_contents to get the content in post mode:

The code is as follows: Copy code
$ 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. 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.

The code is as follows: Copy code

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;
}
}
// Obtain the html part of the url and remove the 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;
}
  

5. Use the fsockopen function to open the url and obtain the complete data in POST mode, including the header and body.

The code is as follows: Copy code
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 = '000000 ';
 
// 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;
}

 

6. Before using the curl library, you may need to check whether php. ini has enabled the curl extension.

The code is as follows: Copy code

$ Ch = curl_init ();
$ Timeout = 5;
Curl_setopt ($ ch, CURLOPT_URL, 'http: // www.111cn.net /');
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
$ File_contents = curl_exec ($ ch );
Curl_close ($ ch );
 
Echo $ file_contents;

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.