Copy codeThe Code is as follows:
<? Php
/**
* Socket version
* Usage:
* $ Post_string = "app = socket & version = beta ";
* Request_by_socket ('jb51. net', '/restServer. php', $ post_string );
*/
Function request_by_socket ($ remote_server, $ remote_path, $ post_string, $ port = 80, $ 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
* Usage:
* $ Post_string = "app = request & version = beta ";
* Request_by_curl ('HTTP: // jb51.net/restserver.php', using 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
* Usage:
* $ Post_string = "app = request & version = beta ";
* Request_by_other ('HTTP: // jb51.net/restserver.php', using post_string );
*/
Function request_by_other ($ remote_server, $ post_string ){
$ Context = array (
'Http' => array (
'Method' => 'post ',
'Header' => 'content-type: application/x-www-form-urlencoded '."".
'User-Agent: Jimmy's POST Example beta '."".
'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;
}
?>