【问题标题】:file_get_contents() returns an empty string when authenticating, otherwise finefile_get_contents() 验证时返回一个空字符串,否则没问题
【发布时间】:2011-05-10 18:45:52
【问题描述】:

所以,我是使用 API 和 PHP 的新手,所以请耐心等待。我正在尝试通过 GET 参数(如 API 文档中的建议)访问 API 以对站点进行身份验证。我正在尝试使用 file_get_contents() 返回它返回的身份验证令牌。重定向到 URL 以 XML 格式输出令牌。

但是,当我执行以下操作时,它返回一个空字符串。如何返回完整的 XML 输出?

$token = file_get_contents('http://example.com/api.asp?cmd=logon&email=xxx@something.com&password=s0mep@SSword');
var_dump($token);

我得到以下输出:

string(116) ""

关于我做错了什么有什么想法吗?

【问题讨论】:

  • API返回的数据是什么格式,JSON,XML?? print_r($token) 给出了什么?
  • 另外,添加使用浏览器直接访问 API 有什么好处?

标签: php api fogbugz


【解决方案1】:

尝试做:

$token = str_replace("<", "&lt;", $token);
$token = str_replace(">", "&gt;", $token);

vd($token);

发生这种情况是因为您的浏览器将您正在读取的 XML 解释为 HTML 并将 XML 读取为标签。 (您可以告诉字符串不为空,因为vd,我假设它是var_dump 的别名告诉您字符串的长度是116。)替换&lt; 和&gt;使用其有效的 HTML 实体应该可以解决这个问题。

【讨论】:

  • 或者只是替换
【解决方案2】:

假设API返回XML,可以在PHP中使用SimpleXML直接解析文件:

 <?php
 $token = simplexml_load_file('http://example.com/api.asp?cmd=logon&email=xxx@something.com&password=s0mep@SSword');
 vd($token);
 ?>

【讨论】:

    猜你喜欢
    • 2011-05-09
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    相关资源
    最近更新 更多