error in loading url in simplexml_load_file - php

I have an rss feed which i would like to read from in my page, i have a local copy of the feed which reads fine but i am required to use the online version. From what i can see i am doing this correctly:
$url ='http//www.numyspace.co.uk/~cgel1/holidays/holidays.xml';
$holidayDoc = simplexml_load_file($url);
However i am met with the following error:
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O
warning : failed to load external entity
"http//www.numyspace.co.uk/~cgel1/holidays/holidays.xml"
Why is this not working?

$use_errors = libxml_use_internal_errors(true);
$xml = simplexml_load_file("http://www.numyspace.co.uk/~cgel1/holidays/holidays.xml");
if (!$xml) {
//throw new Exception("Cannot load xml source.\n");
}
libxml_clear_errors();
libxml_use_internal_errors($use_errors);
You missed a colon

Related

PHP no response when XML validation with XSD

I need to validate some xml-files with xsd files in a php application. The problem I'm having is when I use domdocument or xmlreader, I keep getting a blank page (ERR_EMPTY_RESPONSE) when executing schema validation. I get no errors at all.
$reader = new XMLReader();
$reader->open("../xml/testxml.xml");
echo $reader->setSchema("../xml/validation.xsd") ? 'valid' : 'invalid';
$doc = new DOMDocument();
$doc->load("../xml/testxml.xml");
echo $doc->schemaValidate("../xml/validation.xsd") ? 'valid' : 'invalid';
Both files & paths of the xml/xsd exists. The php version is 5.3.1 & libxml2 version is 2.7.3 and I'm working in an OSX environment.
UPDATE: WORKAROUND
I have found a way to get it working. Instead of using load($path) & schemaValidate($path), I have used loadXml($string) & schemaValidateSource($string). So instead of giving the file path in the load function I load the content manually via file_get_contents() and give it to the loadXml function. Same for the xsd file.
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadXml(file_get_contents("../xml/testxml.xml"));
echo $doc->schemaValidateSource(file_get_contents("../xml/validation.xsd")) ? 'valid' : 'invalid';

PHP transform xml string to XSLT

I'm trying to run an xslt script over a xml response. But I'm having problems getting PHP/XSLT to understand the response. My PHP looks like this, the $data variable contains the XML
$movies = new SimpleXMLElement($data);
// Load the XML source
$xml = new DOMDocument;
$xml->load($movies);
$xsl = new DOMDocument;
$xsl->load('/var/www/html/app/views/xslt/rdf.xsl');
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
$res = $proc->transformToXML($xml);
I'm getting
production.ERROR: exception 'ErrorException' with message 'DOMDocument::load(): I/O warning : failed to load external entity "/var/www/public/
"'
as my error. I've tried using simplexml_load_file and simplexml_load_string but have similar results.

XML load fails or delay on loading

I am reading an xml file and I am loading it in my php file as
$xml = simplexml_load_file($url);
where $url is an external link.
How can I echo if the url fails to load & if it is not responding at a specific time?

DOMDocument load php file?

I have a php file, with xml headers, that i wish my script to load.
$doc = new DOMDocument();
$doc->load( 'xmlscript.php' );
Will not work, only files with *.xml. How can i make it load the file, like it is a xml file?
Since it's the data from db, that it shows in xml format, the file is in .php.
You'll have to get the output from the PHP file, and parse that.
ob_start();
include 'xmlscript.php';
$result = ob_get_clean();
$doc = new DOMDocument();
$doc -> loadXml($result);
Something like that may help.

loading XML string into Xslt sheet

I am trying to load a xml document I created using PHP and DOM into a xslt sheet, but having no luck.
$xml_string = $doc->saveXML();
//echo $xml_string;
$xml = new DOMDocument;
$xml->load($xml_string);
$xsl = new DOMDocument;
$xsl->load('musicInformation.xslt');
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
echo $proc->transformToXML($xml);
I created a xml file based off some data extracted from a database and instead of saving it as an actual document I saved it as a string, I put the string into the xslt sheet and this error occurred
Warning: I/O warning : failed to load
external entity Warning: xpath.c:11079
Internal error: document without root
in
/home/dd615/public_html/webservice.php
on line 73
Any help would be much appreciated.
A string is not XML.
Valid XML needs a root element (that is, a single element that wraps all other elements in the document, apart from the XML declaration).
Such as this:
<?xml version="1.0" ?>
<root>
<element></element>
<element></element>
...
</root>
If you have multiple such roots, the XML is not valid and will fail to load.

Categories