- /**
- * Socket version
- * How to use:
- * $post _string = "App=socket&version=beta";
- * Request_by_socket (' www.1bo8.cn ', '/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\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://www.1bo8.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://www.1bo8.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;
- }
- ?>
Copy Code |