Code:/* @desc: Simulates the GET, post, JSON asynchronous request data @param method request type Get|post|json@param URL request URL address, such as: bulk mail @param data request */function Sock _send ($method, $url, $data =array ()) {$url = ' http://' $url; if (Strtolower ($method) = = ' Get ') {$query = Http_build_query ($ data); $info = Parse_url ($url); $fp = Fsockopen ($info ["host"], $errno, $ERRSTR, 8); $head = "GET". $info [' Path ']. "?". $info ["Query"].trim ($query). " Http/1.0\r\n "; $head. = "Host:". $info [' Host ']. " \ r \ n "; $head. = "connection:close\r\n\r\n"; $write = fputs ($fp, $head); return $write;} ElseIf (Strtolower ($method) = = ' Post ') {$query = Http_build_query ($data); $info = Parse_url ($url); $fp = Fsockopen ($info ["host"], $errno, $ERRSTR, 8); $head = "POST". $info [' Path ']. "?". $info ["Query"]. " Http/1.0\r\n "; $head. = "Host:". $info [' Host ']. " \ r \ n "; $head. = "referer:http://". $info [' Host ']. $info [' Path ']. \ r \ n "; $head. = "content-type:application/x-www-form-urlencoded\r\n"; $head. = "Content-length:". strlen (Trim ($query)). " \ r \ n "; $head. = "connection:close\r\n\r\n"; $head. = Trim ($query); $write = fputs ($fp, $head); return $write;} ElseIf (Strtolower ($method) = = ' json ') {$query = Json_encode ($data); $info = Parse_url ($url); $fp = Fsockopen ($info ["host"], $errno, $ERRSTR, 8); $head = "POST". $info [' Path ']. "?". $info ["Query"]. " Http/1.0\r\n "; $head. = "Host:". $info [' Host ']. " \ r \ n "; $head. = "referer:http://". $info [' Host ']. $info [' Path ']. \ r \ n "; $head. = "content-type:application/json\r\n"; $head. = "Content-length:". strlen (Trim ($query)). " \ r \ n "; $head. = "connection:close\r\n\r\n"; $head. = Trim ($query); $write = fputs ($fp, $head); return $write;} else{return false;}}
Test:$ret = sock_send(‘json‘,‘192.168.8.81‘,array( ‘name‘=>‘lee‘));if($ret){echo ‘发送成功‘;}
Output:发送成功
PHP Asynchronous request data (forwarding requests to be processed elsewhere)