【问题标题】:'&' becomes '&' when trying to get contents from a URL'&' 在尝试从 URL 获取内容时变为 '&'
【发布时间】:2013-06-07 02:01:24
【问题描述】:

我使用相同的算法运行我的 WebServer 数月,通过使用这行代码获取 URL 的内容:

$response = file_get_contents('http://femoso.de:8019/api/2/getVendorLogin?' . http_build_query(array('vendor'=>$vendor,'user'=>$login,'pw'=>$pw),'','&'));

但现在肯定发生了一些变化,因为它突然停止工作了。

在早期,该 URL 看起来应该是这样的:

http://femoso.de:8019/api/2/getVendorLogin?vendor=100&user=test&pw=test

但是现在我在我的 nginx 日志中收到一个错误,说我请求了以下 URL,它返回了 403

http://femoso.de:8019/api/2/getVendorLogin?vendor=100&user=test&pw=test

我知道目标服务器上发生了一些变化,但我认为这不应该影响我吗?!

我已经花费了数小时阅读和搜索 Google 和 Stackoverflow,但所有建议的方法都是

urlencode() 或 htmlspecialchars() 等...

对我没用。

为了您的信息,环境是一个 zend 应用程序,我的一端有一个 nginx 服务器,另一端有一个 php webservice 和 apache。

就像我说的,它改变了,我这边没有任何改变!

谢谢

【问题讨论】:

  • 有趣。更改 arg_separator(http_build_query 的第三个参数)对生成的 url 有影响吗?
  • 如果我将 arg_seperator 更改为 '&'例如,我使用 &user 等获取 URL ...
  • 这表明某处正在进行一些后期处理。也许有一个整洁的输出缓冲区改变了 url。出于好奇,生成的 HTML 是显示实体还是只有 nginx 告诉你?
  • 我对后期处理也有同样的感觉,但由于我没有改变任何东西,我认为这可能是远程服务器的问题?! nginx 告诉我我请求了这个错误的 url,并说它返回了 403 导致 php 错误
  • @Evils,您的服务器当前运行的是什么 PHP 版本? arg_separator 参数已在 5.1.2 中添加。不过,(意外)降级的可能性不大..

标签: php html apache nginx


【解决方案1】:

让我们找出罪魁祸首!


1) 是http_build_query 吗?尝试替换:

'http://femoso.de:8019/api/2/getVendorLogin?' . http_build_query(array('vendor'=>$vendor,'user'=>$login,'pw'=>$pw)

与:

"http://femoso.de:8019/api/2/getVendorLogin?vendor={$vendor}&user={$login}&pw={$pw}"


2) 是否有某种后处理?尝试将'&' 替换为chr(38)


3) 可以尝试一下play 与 cURL 一起使用吗?

$ch = curl_init();

curl_setopt_array($ch, array(
    CURLOPT_URL => 'http://femoso.de:8019/api/2/getVendorLogin?' . http_build_query(array('vendor'=>$vendor,'user'=>$login,'pw'=>$pw),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER => true, // include response header in result
    //CURLOPT_FOLLOWLOCATION => true, // uncomment to follow redirects
    CURLINFO_HEADER_OUT => true, // track request header, see var_dump below
));

$data = curl_exec($ch);
curl_close($ch);

var_dump($data, curl_getinfo($ch, CURLINFO_HEADER_OUT));
exit;

【讨论】:

  • 这太奇怪了...第一个选项带来与之前相同的输出 &而不是 & 第二个选项带来一个 chr(38) 而不是一个 $ ...陌生的世界
  • 万一,你没有在 chr(38) 周围留下引号吗?
  • 这就是我现在尝试的方法 $response = file_get_contents('femoso.de:8019/api/2/getVendorLogin?vendor=' . $vendor . "chr(38)" . 'user=' . $login . "chr(38)" . 'pw=' . $pw );
  • 如果我没有任何引号,如 $response = file_get_contents('femoso.de:8019/api/2/getVendorLogin?vendor=' . $vendor . chr(38) . 'user=' . $login . chr(38) . ' pw=' . $pw );我会得到一个 &再次代替 &
  • 假设您没有犯错,看起来问题不在 PHP 中,因此您的服务器或远程应用程序以某种方式损坏了。我在答案中添加了一个带有 cURL 的有趣游乐场。
【解决方案2】:

听起来您的arg_separator.output 在您的php.ini 中设置为"&"。要么注释掉那一行,要么改成"&"

【讨论】:

    【解决方案3】:

    我不是专家,但这是计算机读取地址的方式,因为它是一个特殊字符。有编码的东西。简单的解决方法是使用 str_replace() 进行过滤。类似的东西。

    【讨论】:

    • 试过了,完全没有效果
    猜你喜欢
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 2011-09-21
    • 2011-05-12
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多