【问题标题】:HTTP/1.1 400 Bad Request in file_get_contentsfile_get_contents 中的 HTTP/1.1 400 错误请求
【发布时间】:2014-10-24 19:52:34
【问题描述】:

我有一个链接http://www.example.com/Movies.aspx?movname=Raja Natwarlal

这是我的代码

file_get_contents($link, false, $context);

它给了我这个错误

failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

我也试过这个代码

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get the code of request
curl_close($ch);

它给了我 $httpCode=400

【问题讨论】:

  • 首先可能值得尝试正确地 http 转义 url 中的空白,其次可能是服务器拒绝了请求并且确实发送了错误 400。
  • 我也试过 urlencode() 但没有运气
  • 在整个 url 上使用 urlencode() 没有任何意义。这不是我的建议。
  • 试试这些变体:http://www.example.com/Movies.aspx?movname=Raja+Natwarlalhttp://www.example.com/Movies.aspx?movname=Raja%20Natwarlal...
  • 不客气 :-) 我转发了这个建议作为答案。

标签: php http file-get-contents


【解决方案1】:

您发布的网址在技术上无效。如果您输入这样的 URL,浏览器会处理这些问题,但在技术层面上,您必须自己处理这些细节。

在这种情况下,urls 请求参数中有一个空格。此类字符在 URL 中无效。因此你必须逃避它。试试这些变体:

请注意,您不能简单地使用 urlencode() 之类的函数来处理整个 URL。这也将逃避诸如斜线之类的事情。该函数旨在转义一个字符串,以便它可以用作 URL inside 的单个标记。

【讨论】:

    猜你喜欢
    • 2017-07-21
    • 2017-08-07
    • 1970-01-01
    • 2015-04-10
    • 2022-10-02
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多