PHP中如何发送HTTP请求

2025-03-12 23:23:26
推荐回答(2个)
回答1:

我觉得你这个正确,一眼看不出错在哪里,如果是我我就不检查了,我下面粘贴一个我使用正常的函数,你直接调用函数就可以了,调用语句可以这样:
$A=trim(urlencode($_REQUEST['A']));
$B=trim(urlencode($_REQUEST['B']));
$params="A=$A&B=$B";
list($body,$header)=http_request('http://localhost/test/re.php','POST',$params);

如果你无需检查返回结果,那就这样也可以:
http_request('http://localhost/test/re.php','POST',$params);

函数定义如下:
//执行HTTP请求
function http_request($url,$method='GET',$data='',$cookie='',$refer=''){
$header='';
$body='';
$newcookie='';
if (preg_match('/^http:\/\/(.*?)(\/.*)$/',$url,$reg)){$host=$reg[1]; $path=$reg[2];}
else {outs(1,"URL($url)格式非法!"); return;}
$http_host=$host;
if (preg_match('/^(.*):(\d+)$/', $host, $reg)) {$host=$reg[1]; $port=$reg[2];}
else $port=80;
$fp = fsockopen($host, $port, $errno, $errstr, 30);
if (!$fp) {
outs(1,"$errstr ($errno)\n");
} else {
fputs($fp, "$method $path HTTP/1.1\r\n");
fputs($fp, "Host: $http_host\r\n");
if ($refer!='') fputs($fp, "Referer: $refer\r\n");
if ($cookie!='') fputs($fp, "Cookie: $cookie\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: ".strlen($data)."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data . "\r\n\r\n");
$header_body=0;
$chunked_format=0;
$chunked_len=0;
while (!feof($fp)) {
$str=fgets($fp);
//$len=hexdec($str); if ($header_body==1) {echo ">>$str\t$len\n"; $str=fread($fp,$len);echo $str;}
if ($header_body==1){
if ($chunked_format){
if ($chunked_len<=0){
$chunked_len=hexdec($str);
if ($chunked_len==0) break;
else continue;
} else {
$chunked_len-=strlen($str);
if ($chunked_len<=0) $str=trim($str);
//elseif ($chunked_len==0) fgets($fp);
}
}
$body.=$str;
}
else if ($str=="\r\n") $header_body=1;
else {
$header.=$str;
if ($str=="Transfer-Encoding: chunked\r\n") $chunked_format=1;
if (preg_match('|Set-Cookie: (\S+)=(\S+);|',$str,$reg)) $newcookie.=($newcookie==''?'':'; ').$reg[1].'='.$reg[2];
}
}
fclose($fp);
}
$GLOBALS['TRAFFIC']+=414+strlen($url)+strlen($data)+strlen($header)+strlen($body);
if (preg_match('/^Location: (\S+)\r\n/m',$header,$reg)) {
if (substr($reg[1],0,1)!='/'){
$path=substr($path,0,strrpos($path,'/')+1);
$path.=$reg[1];
} else $path=$reg[1];
if ($newcookie) $cookie=$newcookie;
return http_request('http://'.$http_host.$path,'GET','',$cookie,$url);
}
return array($body, $header, $newcookie);
}

回答2:

看起来你的代码正确,不知道你有什么问题。
这个方法不错,但是最好用一个封装好的类。

比如http_client之类的,网上这样的类挺多了,你可以搜索一下。
当然直接用socket也可以。