修改通用 REST Helper PHP 示例代码以支持 XML DOM
我在 HTTP 中找到了这个示例 PHP 源代码来自 PHP 的 POST,没有 cURL
我需要一些帮助来修改示例 PHP 源以支持 XML DOM 来操作 REST API。
我认为如果我将下面的 XML 部分的 CASE 语句从 更新为 ,
$r = simplexml_load_string($res);
它
$r = new DOMDocument();
$r->load($res);
会起作用,但事实并非如此。 :(
任何帮助将不胜感激。
function rest_helper($url, $params = null, $verb = 'GET', $format = 'xml')
{
$cparams = array(
'http' => array(
'method' => $verb,
'ignore_errors' => true
)
);
if ($params !== null) {
$params = http_build_query($params);
if ($verb == 'POST') {
$cparams['http']['content'] = $params;
} else {
$url .= '?' . $params;
}
}
$context = stream_context_create($cparams);
$fp = fopen($url, 'rb', false, $context);
if (!$fp) {
$res = false;
} else {
// If you're trying to troubleshoot problems, try uncommenting the
// next two lines; it will show you the HTTP response headers across
// all the redirects:
// $meta = stream_get_meta_data($fp);
// var_dump($meta['wrapper_data']);
$res = stream_get_contents($fp);
}
if ($res === false) {
throw new Exception("$verb $url failed: $php_errormsg");
}
switch ($format) {
case 'json':
$r = json_decode($res);
if ($r === null) {
throw new Exception("failed to decode $res as json");
}
return $r;
case 'xml':
$r = simplexml_load_string($res);
if ($r === null) {
throw new Exception("failed to decode $res as xml");
}
return $r;
}
return $res;
}
I found this example PHP source code at HTTP POST from PHP, without cURL
I need some help modifying the example PHP source to support XML DOM for manipulating a REST API.
I thought that if I update the CASE statement for the XML section below from
$r = simplexml_load_string($res);
to
$r = new DOMDocument();
$r->load($res);
that it would work but it doesn't. :(
Any help would be appreciated.
function rest_helper($url, $params = null, $verb = 'GET', $format = 'xml')
{
$cparams = array(
'http' => array(
'method' => $verb,
'ignore_errors' => true
)
);
if ($params !== null) {
$params = http_build_query($params);
if ($verb == 'POST') {
$cparams['http']['content'] = $params;
} else {
$url .= '?' . $params;
}
}
$context = stream_context_create($cparams);
$fp = fopen($url, 'rb', false, $context);
if (!$fp) {
$res = false;
} else {
// If you're trying to troubleshoot problems, try uncommenting the
// next two lines; it will show you the HTTP response headers across
// all the redirects:
// $meta = stream_get_meta_data($fp);
// var_dump($meta['wrapper_data']);
$res = stream_get_contents($fp);
}
if ($res === false) {
throw new Exception("$verb $url failed: $php_errormsg");
}
switch ($format) {
case 'json':
$r = json_decode($res);
if ($r === null) {
throw new Exception("failed to decode $res as json");
}
return $r;
case 'xml':
$r = simplexml_load_string($res);
if ($r === null) {
throw new Exception("failed to decode $res as xml");
}
return $r;
}
return $res;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确的形式是:
请参阅 DOMDocument::loadXML 的文档
DOMDocument::load()
方法从文件加载。The correct form is:
See the documentation for DOMDocument::loadXML The
DOMDocument::load()
method loads from a file.