Tag:php Async code
/** * send a GET request for an asynchronous HTTP protocol without caring about the results * @param $url * @param $errno * @param $errstr * @param $time _out */ static public function GETASN ($url, $errno = ", $errstr =", $time _out = 5) { //remove spaces from the URL, if you can format the URL, it might be better $url = str_replace (' ', ', $url); $arr = parse_url ($url); $arr [' Port '] | | $arr [' Port '] = 80; $fp = fsockopen ($ arr[' host ', $arr [' Port '], $errno, $ERRSTR, $tIme_out); if (! $fp) { return $errno. " ". $errstr; } $arr [' query '] && $arr [' Query '] = '? '. $arr [' query ']; $out = ' get '. $arr [' path ']. $arr [' Query ']. http/1.1\r\n "; $out .= " host: ". $arr [' Host ‘]." \ r \ n "; $out .= " connection: close\r\n\r\n "; fwrite ($fp, $out); fclose ($FP); } /** * Asynchronous post * @param    $url * @param $post _arr * @ param $errno * @param $errstr * @param $time _out */ static     FUNCTION POSTASN ($url, $post _arr, $errno = ", $errstr =", $time _out = 5) { $arr = parse_url ($url); $arr [' Port '] | | $arr [' Port '] = 80; $fp = Fsockopen ($arr [' Host '], $arr [' Port '], $errno, $errstr, $time _out); if (! $fp) { return $errno. " ". $errstr; } $post _data = ""; if ($post _arr) { //can also be used here http_build_query () function to encode the contents of the Post foreach ($post _arr as $key => $val) { $post _data .= urlencode ($key) . " = ". urlencode ($val)." & "; } $post _data = substr ($post _data, 0,-1); } $data _len = strlen ($post _data); $arr [' Query '] && $arr [' QuerY '] = '?. $arr [' query ']; $out = ' post '. $arr [' path ']. $arr [ ' Query ']. " http/1.1\r\n "; $out .= " host: ". $arr [' Host ‘]." \ r \ n "; $out .= " content-type:application/ x-www-form-urlencoded\r\n "; $out .= " connection: close\r\n "; $out .= " Content-Length: $data _len\r\n\r\n "; $out .= $post _data." \ r \ n "; fwrite ($fp, $out); fclose ($fp); }