PHP Tutorial: Three ways to post data _php tutorial

Source: Internet
Author: User
PHP has three ways to post data, namely curl, socket, file_get_contents:

Take a look at the code below:

/**
* Socket version
* How to use:
* $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 = up, $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
* How to use:
* $post _string = "App=request&version=beta";
* Request_by_curl (' http://facebook.cn/restServer.php ', $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
* How to use:
* $post _string = "App=request&version=beta";
* Request_by_other (' http://facebook.cn/restServer.php ', $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;
}

?>

http://www.bkjia.com/PHPjc/363900.html www.bkjia.com true http://www.bkjia.com/PHPjc/363900.html techarticle PHP has three ways to post data, respectively, curl, socket, file_get_contents: see 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.