【问题标题】:Obtaining XML through API call通过API调用获取XML
【发布时间】:2010-08-22 13:35:42
【问题描述】:

我正在尝试通过 API 发出调用,以便删除用户属性。当我转到ourwiki.com/@api/users/=john_smith%40ourwiki.com/properties 时,它会返回包含所有用户属性的 XML。

我正在尝试将该 XML 存储在变量 $xmlString 中,然后我应该能够循环获取属性并依次删除。用户属性是键值,其中键和值可以是任何值,因此遗憾的是无法知道所有选项。

到目前为止的代码:

$delete = "http://www.ourwiki.com/@api/DELETE:users/$user_id/properties/%s";
$xmlString = file_get_contents('ourwiki.com/@api/users/=john_smith%40ourwiki.com/properties')
$xml = new SimpleXMLElement($xmlString);

foreach($xml->property as $property) {
  $name = $property['name']; // the name is stored in the attribute
  file_get_contents(sprintf($delete, $name));
}

由于某种原因,file_get_contents 似乎无法获取 XML。我什至确保在环境中启用了 allow_url_fopen 。对此的任何帮助将不胜感激。

【问题讨论】:

  • 您是否收到错误/警告消息并将 error_reporting 设置为 E_ALL? $xmlString (var_dump($xmlString);) 的值是多少?
  • 假设我只是在分配变量后添加 var_dump($xmlString),它返回 bool(false)。

标签: php xml api


【解决方案1】:

试试

$xmlString = file_get_contents("http://ourwiki.com/@api/users/=john_smith%40ourwiki.com/properties");

在第二行。由于一开始不包括协议,PHP 正在服务器文件系统中寻找文件。

编辑:

您说var_dump($xmlString) 返回错误。来自文档:

此函数类似于 file(),不同之处在于 file_get_contents() 以字符串形式返回文件,从指定的偏移量开始直到 maxlen 字节。 失败时, file_get_contents() 将返回 FALSE。

这意味着 PHP 不能 GET 来自该 URL 的任何数据。

尝试使用

$xmlString = file_get_contents(urlencode('http://ourwiki.com/@api/users/=john_smith@ourwiki.com/properties'));

【讨论】:

  • 对不起,当我写的时候我在帖子中省略了它,但我在实际测试脚本时确实包含了协议。不过好像没什么效果。
  • VolkerK 询问的var_dump 的结果是什么?
  • 我刚刚在上面添加了一条评论,但它返回 bool(false)
  • 显然无法获取。这就是我发布问题的原因。使用浏览器时,我可以毫无问题地获取 XML。我还确保通过 php.ini 启用了 allow_url_fopen。还有什么你知道我可以检查的吗?或者这是不可能的?
  • 查看上次编辑。将%40 转回@ 字符,并通过urlencode 运行整个URL。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 1970-01-01
  • 1970-01-01
  • 2015-02-01
  • 2018-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多