php 发出http请求

bogu 2006-05-22 02:02:13
在PHP页面中,怎样向另一个页面发送http请求(http://www.aaa.com/aa.htm?test=bbb,主要是向另一页面传递参数bbb),但同时要保留显示原先的PHP页面。(用Header("Location: http://www.aaa.com/aa.htm?test=bbb"),就直接跳到另一页面,没有保留显示原先的PHP页面),有什么好办法,thanks
...全文
475 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
颓废的老猫 2006-05-25
  • 打赏
  • 举报
回复
echo "window.open('http://www.aaa.com/aa.htm?test=bbb')";
黑夜路人 2006-05-25
  • 打赏
  • 举报
回复
<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";

fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
zeroleonhart 2006-05-24
  • 打赏
  • 举报
回复
用js的window.open()不行吗?可以新开窗口或是向已打开的窗口传递参数
nba23 2006-05-24
  • 打赏
  • 举报
回复
强贴收藏
indexroot 2006-05-23
  • 打赏
  • 举报
回复
<?php
//用这个也行
$r=new HTTPRequest("http://www.aaa.com/aa.htm?test=bbb","GET");
/**
* @author:love01px增加POST功能
* 支持GET | POST
* HTTP | HTTPS
*/
class HTTPRequest
{
var $mArray=array("GET","POST");
var $_fp; // HTTP socket
var $_url; // full URL
var $_host; // HTTP host
var $_protocol; // protocol (HTTP/HTTPS)
var $_uri; // request URI
var $_method; // method
var $_query; // query
var $_port; // port

// scan url
function _scan_url()
{
$req = $this->_url;

$pos = strpos($req, '://');
$this->_protocol = strtolower(substr($req, 0, $pos));

$req = substr($req, $pos+3);
$pos = strpos($req, '/');
if($pos === false)
$pos = strlen($req);
$host = substr($req, 0, $pos);

if(strpos($host, ':') !== false)
{
list($this->_host, $this->_port) = explode(':', $host);
}
else
{
$this->_host = $host;
$this->_port = ($this->_protocol == 'https') ? 443 : 80;
}

$this->_uri = substr($req, $pos);
if($this->_uri == '')
$this->_uri = '/';
}

// constructor
function HTTPRequest($url,$method="GET",$query="")
{
$this->_method=(in_array($method,$this->mArray))?$method:"GET";
$this->_url = $url;
$this->_query = $query;
$this->_scan_url();
}

// download URL to string
function DownloadToString()
{
$crlf = "\r\n";

// generate request
$req = $this->_method ." ". $this->_uri . ' HTTP/1.0' . $crlf
. 'Host: ' . $this->_host . $crlf.(($this->_method=="POST")?("Content-type: application/x-www-form-urlencoded\r\nUser-Agent: Mozilla 4.0\r\nContent-length: ".strlen($this->_query)."\r\nAccept: */*\r\n\r\n$this->_query".$crlf.$crlf):$crlf);
// fetch
$this->_fp = fsockopen(($this->_protocol == 'https' ? 'ssl://' : '') . $this->_host, $this->_port, $errno, $errstr, 30);
fwrite($this->_fp, $req);
while(is_resource($this->_fp) && $this->_fp && !feof($this->_fp))
$response .= fread($this->_fp, 1024);
fclose($this->_fp);

// split header and body
$pos = strpos($response, $crlf . $crlf);
if($pos === false)
return($response);
$header = substr($response, 0, $pos);
$body = substr($response, $pos + 2 * strlen($crlf));

// parse headers
$headers = array();
$lines = explode($crlf, $header);
foreach($lines as $line)
if(($pos = strpos($line, ':')) !== false)
$headers[strtolower(trim(substr($line, 0, $pos)))] = trim(substr($line, $pos+1));

// redirection?
if(isset($headers['location']))
{
$http = new HTTPRequest($headers['location']);
return($http->DownloadToString($http));
}
else
{
return($body);
}
}
}
?>
fwqlove 2006-05-22
  • 打赏
  • 举报
回复
呀。你真是太浪费分数了。

我现在拼命回复只因我当初也是为同样的问题送人100分。

http://community.csdn.net/Expert/TopicView.asp?id=4220726
ice_berg16 2006-05-22
  • 打赏
  • 举报
回复
file_get_contents("http://www.aaa.com/aa.htm?test=bbb");
这样就行了
我看你有戏 2006-05-22
  • 打赏
  • 举报
回复
$result=file_get_contents("http://www.163.com"); get方法
//$result = GetFileRow("http://localhost/1.txt");
echo $result;
return;

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧