【问题标题】:file_get_contents failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in jsonfile_get_contents 无法打开流:HTTP 请求失败! json 中的 HTTP/1.1 400 错误请求
【发布时间】:2017-08-07 15:42:57
【问题描述】:

在 file_get_content 上出现错误,它将 & 替换为 &,因此我收到 d 错误。我得到的数据是 json 格式的。

原始网址为:xyz.abc.com/index.php/DeviceDetails/GetDeviceHistory?tracking_id=70&start_time=2017-03-05 13:14:50&end_time=2017-03-08 13:14:50

<b>Warning</b>:  file_get_contents(http://xyz.abc.com/index.php/DeviceDetails/GetDeviceHistory?tracking_id=70&amp;start_time=2017-03-05 13:14:50&amp;end_time=2017-03-08 13:14:50): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
     in
    <b>/home/abc/public_html/php/service.php</b> on line
    <b>1855</b>
    <br />
    {"Status":"false","message":"No data found"}

请帮我解决这个错误

【问题讨论】:

  • 你能提供更多关于你的项目或代码的信息吗?
  • 它只是将错误消息中的&amp;amp; 替换为&amp;amp;。这个问题中没有足够的信息给我们提供任何帮助
  • 未找到数据尝试其他日期范围
  • @RiggsFolly 这是我的代码 $tracking_id = $_REQUEST['sharedid']; $start_time = $_REQUEST['start_time']; $end_time = $_REQUEST['end_time']; $url = "xyz.abc.com/index.php/DeviceDetails/…"; $result = file_get_contents($url); $restvm = json_decode($result,true);
  • @RiggsFolly 我用 & 得到了结果,但是 file_get_contents 正在将 & 变成 &

标签: php json warnings file-get-contents


【解决方案1】:

确保您的实际 URL http://https:// 开头,并在调用 file_get_contents 时使用引号

file_get_contents("http://your_URL_here")

【讨论】:

    【解决方案2】:

    在将 URL 传递给 file_get_contents() 之前,尝试在 URL 上运行 PHP html_entity_decode():http://php.net/manual/en/function.html-entity-decode.php(正如 Kuldeep Singh 上面所说,确保将 URL 作为字符串传递:D)

    我遇到过很多很多类似的问题。可能有很多原因,缺少标头、无效的用户代理、错误的 php.ini 配置等。

    我强烈建议您使用 cURL 库,该库允许您自定义请求的标头、端口等,以便您可以正确调试。这是我使用的自定义 PHP cURL 库:https://gist.github.com/schwindy/e5798405d0b6269945bdf037a58f6a4d

    希望其中的一些帮助。祝你好运。

    【讨论】:

      猜你喜欢
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 2020-12-18
      • 2016-06-02
      • 2012-07-26
      • 2012-12-31
      相关资源
      最近更新 更多