创建 php 代理时出现问题

发布于 2024-12-01 05:11:59 字数 1227 浏览 3 评论 0原文

我无法让它工作:

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请持续率性 2024-12-08 05:11:59

如果您希望此功能正常工作,而不依赖浏览器请求页面,请删除所有 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.

猫瑾少女 2024-12-08 05:11:59

啊,你的 cURL 太复杂了,它使用 cookie 和标头来指示请求需要一个图像。
只需尝试这个:

$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);

Ah, your cURL is overly complex, it uses cookies and headers that indicate the request is expecting a an image.
simply try this instead:

$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);

if this still doesn't work, just use firebug to see what are the headers you browser is sending and add them, like this:

$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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文