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