PHP中,直接利用socket将数据POST到指定的地址
function postAsn($url,$post_arr,$errno = '',$errstr='',$time_out = 5) { $result = ''; $arr = parse_url($url); $arr['port'] || $arr['port'] = 80; $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"; try{ $fp = fsockopen($arr['host'],$arr['port'],$errno,$errstr,$time_out); if(!$fp) { return $errno." ".$errstr; } fwrite($fp,$out); //在此也可以不读取响应的内容 while (!feof($fp)) { $t = fgets($fp, 128); $result .= $t; } fclose($fp); }catch(Exception $e){ //在此处理异常 print_r($e); } return $result;}$url = 'http://yourlogin_url/';$post_param = array();$post_param['loginname'] = '';$post_param['loginpass'] = '';$res = postAsn($url,$post_param) ;print_r($res);