PHP Tutorial: three methods of POST data _ PHP Tutorial

Source: Internet
Author: User
PHP Tutorial: three methods of POST data. Php can post data in three ways: Curl, socket, and file_get_contents. for details, see the following code: *** Socket version ** usage: * $ post_stringappsocketversionbeta php can post data in three methods: Curl, socket, and file_get_contents:

See the following code:

/**
* Socket version
* Usage:
* $ Post_string = "app = socket & version = beta ";
* Request_by_socket ('Facebook. cn', '/restServer. php', $ post_string );
* Downloads By http://www.bkjia.com/down
*/
Function request_by_socket ($ remote_server, $ remote_path, $ post_string, $ port = 80, $ timeout = 30 ){
$ Socket = fsockopen ($ remote_server, $ port, $ errno, $ errstr, $ timeout );
If (! $ Socket) die ("$ errstr ($ errno )");

Fwrite ($ socket, "POST $ remote_path HTTP/1.0 \ r \ n ");
Fwrite ($ socket, "User-Agent: Socket Example \ r \ n ");
Fwrite ($ socket, "HOST: $ remote_server \ r \ n ");
Fwrite ($ socket, "Content-type: application/x-www-form-urlencoded \ r \ n ");
Fwrite ($ socket, "Content-length:". strlen ($ post_string) + 8. "\ r \ n ");
Fwrite ($ socket, "Accept: */* \ r \ n ");
Fwrite ($ socket, "\ r \ n ");
Fwrite ($ socket, "mypost = $ post_string \ r \ n ");
Fwrite ($ socket, "\ r \ n ");

$ Header = "";
While ($ str = trim (fgets ($ socket, 4096 ))){
$ Header. = $ str;
}

$ Data = "";
While (! Feof ($ socket )){
$ Data. = fgets ($ socket, 4096 );
}

Return $ data;
}

/**
* Curl version
* Usage:
* $ Post_string = "app = request & version = beta ";
* Request_by_curl ('http: // facebook.cn/restserver.php', then post_string );
*/
Function request_by_curl ($ remote_server, $ post_string ){
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ remote_server );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, 'mypost = '. $ post_string );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
Curl_setopt ($ ch, CURLOPT_USERAGENT, "Jimmy's CURL Example beta ");
$ Data = curl_exec ($ ch );
Curl_close ($ ch );
Return $ data;
}
/**
* Other versions
* Usage:
* $ Post_string = "app = request & version = beta ";
* Request_by_other ('http: // facebook.cn/restserver.php', then post_string );
*/
Function request_by_other ($ remote_server, $ post_string ){
$ Context = array (
'Http' => array (
'Method' => 'post ',
'Header' => 'Content-type: application/x-www-form-urlencoded'. "\ r \ n ".
'User-Agent: Jimmy \'s POST Example beta '. "\ r \ n ".
'Content-length: '. strlen ($ post_string) + 8,
'Content' => 'mypost = '. $ post_string)
);
$ Stream_context = stream_context_create ($ context );
$ Data = file_get_contents ($ remote_server, FALSE, $ stream_context );
Return $ data;
}

?>

Refer to the following code:/*** Socket version ** usage: * $ post_string = app = socketversion = beta...

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.