PHP中如何发送HTTP请求
在HTML上提交参数A和B到send。php中,在send。php中接收到传过来的参数A和B并将这2个参数以http的形式发送给目标地址;一下是我写的代码请各位看看那里有...
在 HTML上 提交参数 A 和 B 到 send。php中,在send。php中接收到传过来的参数 A 和 B 并将这2个参数以 http的形式发送给目标地址;一下是我写的代码 请各位看看 那里有问题 或者 请高手传授其他方法(只能在PHP文件里
发送HTTP请求)
$A=trim(urlencode($_REQUEST['A']));
$B=trim(urlencode($_REQUEST['B']));
$params = "A=$A&B=$B";
$length = strlen($params);
$fp = fsockopen("localhost",80,$errno,$errstr,10) or exit($errstr."--->".$errno);
$header = "POST /test/re.php HTTP/1.1\r\n";
$header .= "Host:localhost\r\n";
$header .= "Referer:localhost/test/re.php\r\n";
$header .= "Content-Length: ".$lenght."\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Connection: Close\r\n\r\n";
$header .= $params."\r\n";
fputs($fp,$header);
fclose($fp); 展开
发送HTTP请求)
$A=trim(urlencode($_REQUEST['A']));
$B=trim(urlencode($_REQUEST['B']));
$params = "A=$A&B=$B";
$length = strlen($params);
$fp = fsockopen("localhost",80,$errno,$errstr,10) or exit($errstr."--->".$errno);
$header = "POST /test/re.php HTTP/1.1\r\n";
$header .= "Host:localhost\r\n";
$header .= "Referer:localhost/test/re.php\r\n";
$header .= "Content-Length: ".$lenght."\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Connection: Close\r\n\r\n";
$header .= $params."\r\n";
fputs($fp,$header);
fclose($fp); 展开
2个回答
展开全部
我觉得你这个正确,一眼看不出错在哪里,如果是我我就不检查了,我下面粘贴一个我使用正常的函数,你直接调用函数就可以了,调用语句可以这样:
$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);
}
$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);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询