创建php代理的问题


Problem with creating php proxy

我不能让这个工作:

<?php
    $url = 'http://someServer.com/save.asp?';
    $count = 0;
    foreach($_POST as $key=>$value) 
    {
        if( $count != 0 ) $url .= "&";
        $url .= urlencode($key).'='.urlencode($value);
        $count++;
    } 
?>
<html>
<head>
<meta http-equiv="refresh" content="0;url=<?php echo $url;?>">
</head>
<body>
</body>
</html>

如果我复制最后$url字符串的输出并将其直接粘贴到浏览器中,它可以正常工作。

所以看起来$url构建正确,但是重定向有一些问题。

//编辑

我正在试着让这个工作:

<?php 

$str = '';
$count = 0;
foreach($_POST as $key=>$value) 
{
    if( $count != 0 ) $str .= "&";
    //$url .= urlencode($key).'='.urlencode($value);
    $str .= $key.'='.$value;
    $count++;
} 

$url = 'http://someDomain.com/acript.asp?'.$str; 

$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
var_dump($result);
?> 

I get: Bad Request

如果你想让它工作,而不依赖于浏览器请求页面,删除所有的HTML并添加:

print file_get_contents($url);

在末尾。如果这对你不起作用(可能有几个原因),你必须使用- http://php.net/manual/en/book.curl.php.

同样要构建正确的url,你不必自己遍历$_POST,使用:http://php.net/manual/en/function.http-build-query.php

PS无论你想做什么,这都可能是错误的解决方案。

啊,你的cURL太复杂了,它使用cookie和header来指示请求期待一个图像。简单地试试这个:

$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
var_dump($result);

如果这仍然不起作用,只需使用firebug查看浏览器正在发送的标头并添加它们,像这样:

$headers = array("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);