【问题标题】:PHP file_get_contents problemPHP file_get_contents 问题
【发布时间】:2011-07-09 20:52:11
【问题描述】:

我遇到了 PHP 的 file_get_contents 命令的问题。

$url = "http://api.rememberthemilk.com/services/rest/".$format.$auth_token.$filter."&api_sig=".$md5.$apikey.$method;

$content = file_get_contents($url);

$array = json_decode($content, true);
$taskname = $array['rsp']['tasks']['list']['taskseries']['name'];
$duedate = $array['rsp']['tasks']['list']['taskseries']['task']['due'];

($format, $auth_token, $filter, $md5, $apikey, $method 已经在脚本中定义了)

当我尝试运行此代码时,会返回此错误:

[function.file-get-contents]:打开流失败:HTTP 请求失败! HTTP/1.1 400 第 101 行的错误请求

第 101 行 = $content, = file_get_contents($url);

如何解决这个问题?谢谢!!!

【问题讨论】:

  • file_get_contents 中没有什么需要修复的。您尝试读取的 URL 不正确。
  • 请显示您尝试阅读的完整网址(当然没有您的身份验证令牌)
  • 也有可能是请求无效(参数错误,你好像串联有点多),或者PHP user_agent被阻塞了。
  • 您可能需要检查 allow_url_fopen 的值。此函数的 php 文档中还可以找到有用的信息作为注释。
  • 这是网址:http://api.rememberthemilk.com/services/rest/?format=json&auth_token=AUTH_TOKEN&filter=dueWithin:"3 days of today"&api_sig=API_SIG&api_key=API_KEY&method=rtm.tasks.getList(我替换了实际的 auth_token、api_sig 和 api_key)

标签: php httprequest file-get-contents


【解决方案1】:

在连接变量后尝试打印 URL。然后将 URL 粘贴到浏览器的地址栏中,看看会出现什么。因为它是一个 Web 服务调用,所以您的浏览器可能不知道如何处理响应。在这种情况下,您可能会使用命令行用户代理“curl”获得更多信息,例如

curl -v 'http://some-url'

curl 内置于 Mac 和其他 *nix 机器中,也可用于 Windows。

【讨论】:

    【解决方案2】:

    这个网址看起来不太好。

    http://api.rememberthemilk.com/services/rest/?format=json&auth_token=AUTH_TOKEN&filter=dueWithin:"今天第 3 天"&api_sig=API_SIG&api_key=API_KEY&method=rtm.tasks.getList

    对令牌进行如下编码:

    $filter = 'filter='.urlencode('dueWithin:"今天的第 3 天"');

    使用 urlencode()。

    【讨论】:

      猜你喜欢
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 2011-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多