修改通用REST助手PHP示例代码以支持XMLDOM
我在 我需要一些帮助来修改示例PHP源代码,以支持XMLDOM来操作RESTAPI 我认为,如果我从修改通用REST助手PHP示例代码以支持XMLDOM,php,http,xmldom,Php,Http,Xmldom,我在 我需要一些帮助来修改示例PHP源代码,以支持XMLDOM来操作RESTAPI 我认为,如果我从 $r = simplexml_load_string($res); 到 它会起作用,但不会:( 任何帮助都将不胜感激 function rest_helper($url, $params = null, $verb = 'GET', $format = 'xml') { $cparams = array( 'http' => array( 'method' =&g
$r = simplexml_load_string($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;
}
正确的形式是:
$r = new DOMDocument();
$r->loadXML($res);
有关从文件加载的方法,请参阅文档。谢谢!这就解决了问题!:)
$r = new DOMDocument();
$r->loadXML($res);