使用simplexml_load_file不工作 [英] simplexml_load_file not working
问题描述
我有这块code的低于我的远程托管服务器工作正常,但心不是出于某种原因,我的本地Linux机器上工作。使用的file_get_contents以及获得RESTful服务香港专业教育学院尝试,但它也将返回false。
有谁知道为什么发生这种情况?
感谢:)
$ xml_data =使用simplexml_load_file(****);如果($ XML == FALSE)
{
回声无法载入XML \\ n; 的foreach(libxml_get_errors()为$错误)
{
回声\\ t的,$无差错>消息;
}
}
由于远程文件访问已在服务器上禁用您收到此错误。这另一种是使用卷曲。
使用我下面的code使用卷曲:
函数produce_XML_object_tree($ raw_XML){
libxml_use_internal_errors(真);
尝试{
$ xmlTree =新的SimpleXMLElement($ raw_XML);
}赶上(例外$ E){
// 出了些问题。
$ ERROR_MESSAGE ='的SimpleXMLElement引发了异常。;
的foreach(libxml_get_errors()为$ ERROR_LINE){
$ ERROR_MESSAGE。=\\ t的。 $ error_line->消息;
}
trigger_error($ ERROR_MESSAGE);
返回false;
}
返回$ xmlTree;
}$ xml_feed_url ='******';
$ CH = curl_init();
curl_setopt($ CH,CURLOPT_URL,$ xml_feed_url);
curl_setopt($ CH,CURLOPT_HEADER,FALSE);
curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);
$ XML = curl_exec($ CH);
curl_close($ CH);$ CONT = produce_XML_object_tree($ XML);
现在使用 $续
作为对象的XML访问不同的节点。
I have this piece of code below which works fine on my remote hosted server, but isnt for some reason working on my local linux machine. Ive tried using file_get_contents as well to get the restful service but it also returns false.
Does anyone know Why this is happening?
thanks :)
$xml_data = simplexml_load_file("****");
if ($xml == FALSE)
{
echo "Failed loading XML\n";
foreach (libxml_get_errors() as $error)
{
echo "\t", $error->message;
}
}
You are getting this error because remote file access has been disabled on your server. An alternative to this is using CURL.
Use my code below to use CURL:
function produce_XML_object_tree($raw_XML) {
libxml_use_internal_errors(true);
try {
$xmlTree = new SimpleXMLElement($raw_XML);
} catch (Exception $e) {
// Something went wrong.
$error_message = 'SimpleXMLElement threw an exception.';
foreach(libxml_get_errors() as $error_line) {
$error_message .= "\t" . $error_line->message;
}
trigger_error($error_message);
return false;
}
return $xmlTree;
}
$xml_feed_url = '******';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_feed_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
curl_close($ch);
$cont = produce_XML_object_tree($xml);
Now use $cont
as an object to access different nodes in the xml.
这篇关于使用simplexml_load_file不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!