- /**
- * 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:application/x-www-form-urlencoded ',
- ' content ' = $postdata,
- ' timeout ' = * + //Time-out (unit: s)
- )
- );
- $context = stream_context_create ($options);
- $result = file_get_contents($url, False, $context);
- return $result;
- }
- //How to use
- $post _data = array(
- ' username ' = ' stclair2201 ',
- ' password ' = ' Handan '
- );
- Send_post (' http://www.qianyunlai.com ', $post _data);
-
- /**
- * Socket version
- * How to use:
- * $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 = up,$timeout = 30) {
- $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;
- }
- $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://www.qianyunlai.com/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, "qianyunlai.com's CURL Example beta");
- $data = curl_exec ($ch);
- Curl_close ($ch);
- return $data;
- }
- ?>
Original address: http://blog.sjzycxx.cn/post/435/
The above describes the PHP send POST request, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.