Summary of several methods for sending GET and POST requests in php-PHP source code

Source: Internet
Author: User
In php, there are many methods for sending GET and POST requests, which have not been carefully summarized. Today we can see several methods for sending GET and POST requests shared by a webmaster. In php, there are many methods for sending GET and POST requests, which have not been carefully summarized. Today we can see several methods for sending GET and POST requests shared by a webmaster.

Script ec (2); script

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:

// 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:
$ 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:
$ 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:
$ 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:

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:
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:

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