您好,登錄后才能下訂單哦!
/** * 發送一個異步http協議的Get請求,不用關心結果 * @param $url * @param $errno * @param $errstr * @param $time_out */ static public function getAsn($url,$errno='',$errstr='',$time_out = 5) { //移除url中的空格,如果可以格式化url,或許會更好 $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); } /** * 異步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){ //在這里還可以使用 http_build_query() 函數,將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); }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。