创建 php 代理时出现问题
我无法让它工作:
<?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 can't get this to work:
<?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>
If I copy the output of the final $url string and paste it in directly in the browser, it works fine.
So it looks like the $url gets built correctly, but there is some problem with the redirection.
// EDIT
I am trying to get this to work:
<?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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望此功能正常工作,而不依赖浏览器请求页面,请删除所有 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
无论您想做什么,这可能都是错误的解决方案。
If you want this to work, without relying on the browser to request the page, remove all the HTML and add:
print file_get_contents($url);
at the end. if this doesn't work for you (and it may not work for sevral reasons) you will have to use - http://php.net/manual/en/book.curl.php.
Also to build the right url, you don't have to iterate over $_POST yourself, use:
http://php.net/manual/en/function.http-build-query.php
PS
what ever you are trying to do, this is probably the wrong solution.
啊,你的 cURL 太复杂了,它使用 cookie 和标头来指示请求需要一个图像。
只需尝试这个:
如果这仍然不起作用,只需使用 firebug 来查看浏览器发送的标头是什么并添加它们,如下所示:
Ah, your cURL is overly complex, it uses cookies and headers that indicate the request is expecting a an image.
simply try this instead:
if this still doesn't work, just use firebug to see what are the headers you browser is sending and add them, like this: