Method One:
/**
* Send POST request
* @param string $url request address
* @param array $post _data post key value pair data
* @return string
* * function Send_post ($url, $post _data) {
$postdata = http_build_query ($post _data);
$options = Array ('
http ' => Array (
' method ' => ' POST ',
' header ' => ' Content-type:ap Plication/x-www-form-urlencoded ',
' content ' => $postdata,
' timeout ' => 15 * 60//Timeout (unit: s)
)
);
$context = Stream_context_create ($options);
$result = File_get_contents ($url, False, $context);
return $result;
}
Use method
$post _data = array (
' username ' => ' stclair2201 ', ' Password ' => ' Handan '
);
Send_post (' http://www.jb51.net ', $post _data);
Method Two: Socket version
<?php/** * Socket Version * Use method: * $post _string = "App=socket&version=beta";
* Request_by_socket (' chajia8.com ', '/restserver.php ', $post _string); * * Function Request_by_socket ($remote _server, $remote _path, $post _string, $port =, $timeout =) { $socket
= Fsockopen ($remote _server, $port, $errno, $errstr, $timeout);
if (! $socket) Die ("$errstr ($errno)");
fwrite ($socket, "POST $remote _path http/1.0");
fwrite ($socket, "User-agent:socket Example");
fwrite ($socket, "HOST: $remote _server");
fwrite ($socket, "content-type:application/x-www-form-urlencoded"); fwrite ($socket, "Content-length:". (Strlen ($post _string) + 8).
"");
fwrite ($socket, "accept:*/*");
fwrite ($socket, "");
fwrite ($socket, "mypost= $post _string");
fwrite ($socket, "");
$header = ""; while ($str = Trim (fgets ($socket, 4096)) { $header. = $Str
&NBSP;&NBSP} $data = ""; while (!feof ($socket)) { $data. = Fgets ($socket, 4096); }
return $data; }?>
Method III: Curl version
<?php
/**
* Curl Version
* Use method:
* $post _string = "App=request&version=beta";
* Request_by_curl (' http://www.jb51.net/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, "jb51.net ' s Curl Example beta");
$data = curl_exec ($ch);
Curl_close ($ch);
return $data;
>
Here are the methods of other netizens:
Class request{public static function post ($url, $post _data = ', $timeout = 5) {//curl $ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_post, 1);
if ($post _data!= ') {curl_setopt ($ch, Curlopt_postfields, $post _data);
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
curl_setopt ($ch, Curlopt_header, false);
$file _contents = curl_exec ($ch);
Curl_close ($ch);
return $file _contents;
The public static function Post2 ($url, $data) {//file_get_content $postdata = http_build_query ($data); $opts = Array (' http ' => Array (' method ' => ' POST ', ' header ' => ' Co
ntent-type:application/x-www-form-urlencoded ', ' content ' => $postdata));
$context = Stream_context_create ($opts);
$result = File_get_contents ($url, False, $context);
return $result; The public static function Post3 ($host, $path, $query, $others = ') {//fsocket $post = ' Post $path http/1.1\r\nhost: $host \
r\n ";
$post. = "content-type:application/x-www-form-";
$post. = "Urlencoded\r\n${others}";
$post. = "User-agent:mozilla 4.0\r\ncontent-length:"; $post. =strlen ($query). "
\r\nconnection:close\r\n\r\n$query ";
$h =fsockopen ($host, 80);
Fwrite ($h, $post); For ($a =0, $r = ';!
$a;) {$b =fread ($h, 8192);
$r. = $b;
$a = (($b = = ")? 1:0);
} fclose ($h);
return $r; }
}
You can choose what suits you according to your needs.