PHP Asynchronous request data (forwarding requests to be processed elsewhere)

Source: Internet
Author: User

  • 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)

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.