Parse XML file from url using php
Seems like there is some redirection problem in the url you are accessing, and most probably simplexml_load_file()
doesn't follow redirects...
So the solution would be to use file_get_contents()
or cUrl
...
As file_get_contents()
is easier, I am showing that only...
The code would have to be something like:
<?php
$xml = simplexml_load_string(file_get_contents('http://example_page.ugu.pl/api/test.xml'));
?>
More:
<?php
$xml = simplexml_load_string(file_get_contents('http://jakdojade.pl/pages/api/outputs/schedules.xml'));
?>
^ That too, totally works!
Reading XML from URL with PHP
If you run the following code, you'll see that you're actually getting JSON as response:
$resp = file_get_contents( 'http://api.autoit.dk/car/GetCarsExtended/391B093F-BB4A-45AA-BEFF-7B33842401EA' );
echo $resp;
Output:
[{"ComId":4712,"Husnr":10,"Navn":"Flexto A/S","By":"Støvring","Postnr":9530,"Adresse":"Juelstrupparken","Email":"info@flexto.dk","Telefon":"96365225","KoeretoejId":641321,"Abs":true,"Accelerationsevne0Til100Kmt":null,"AirbagsAntal":6,"Beskrivelse":"- og meget mere.\nDette er et flexleasingtilbud, udbetaling 45.000,- depot 16.000,- leasingydelse 5.700,- alt excl. moms. Restværdi efter 3 år 121.000 excl. afgift.","BreddeMm":1841,
(etc.)
SimpleXML is not able to convert JSON into an object, so it's obviously throwing an error.
If you want to use the JSON data instead of XML, you can simply decode the JSON:
$data = json_decode($resp, true); // or json_decode($resp) for objects rather than arrays
Part of $data
:
(
[0] => Array
(
[ComId] => 4712
[Husnr] => 10
[Navn] => Flexto A/S
[By] => Støvring
[Postnr] => 9530
[Adresse] => Juelstrupparken
[Email] => info@flexto.dk
If you want the XML data, you'll need to set the appropriate headers when calling file_get_contents
:
$opts = array(
'http' => array(
'header' => "Accept: application/xml"
)
);
$context = stream_context_create($opts);
$response = file_get_contents( 'http://api.autoit.dk/car/GetCarsExtended/391B093F-BB4A-45AA-BEFF-7B33842401EA', NULL, $context );
Output now:
<ArrayOfDealerCarExtended xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/AutoITApis.Controllers"><DealerCarExtended><Abs>true</Abs><Accelerationsevne0Til100Kmt i:nil="true" />
(etc.)
See stream_context_create
for more information on how to set up headers for file_get_contents
.
PHP: read a php url as xml and parse it
One of the best options in my opinion is to use CURL to get the raw XML data from the url:
$curl = curl_init();
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_URL, "http://somedomain.com/frieventexport.php" );
$xml = curl_exec( $curl );
curl_close( $curl );
You can then use DOMDocument to parse the xml:
$document = new DOMDocument;
$document->loadXML( $xml );
I would also recommend using <![CDATA[]>
tags in your XML. Please read the following:
- What does
<![CDATA[]]>
in XML mean? - CDATA Sections in XML
More information about DOMDocument and usage
- PHP.net DOMDocument documentation
- W3Schools DOMDocument example
Read xml data from url using php
You can use simplexml_load_file(string $filename)
to this end. Just pass into this function the full URL such as:
$url="your url";
$xmlinfo = simplexml_load_file($url);
print_r($xmlinfo);
php get xml from remote url not a file
simplexml_load_file()
returns an object rather than the XML string. Even then, with your current code, the XML would be lost within HTML. The following would be equivalent to visiting the URL directly:
header('Content-type: application/xml');
echo file_get_contents('http://cloud.tfl.gov.uk/TrackerNet/PredictionSummary/V');
To make sure you can parse the XML, try this. It loads the XML and prints out the name of the root node - in this case, ROOT:
$xml = simplexml_load_file($url); //retrieve URL and parse XML content
echo $xml->getName(); // output name of root element
Does this help?
read xml file with php
Using DomDocument:
<?php
$str = <<<XML
<currencies>
<currency name="US dollar" code_alpha="USD" code_numeric="840" />
<currency name="Euro" code_alpha="EUR" code_numeric="978" />
</currencies>
XML;
$dom = new DOMDocument();
$dom->loadXML($str);
foreach($dom->getElementsByTagName('currency') as $currency)
{
echo $currency->getAttribute('name'), "\n";
echo $currency->getAttribute('code_alpha'), "\n";
echo $currency->getAttribute('code_numeric'), "\n";
echo "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n";
}
?>
Live DEMO.
Using simplexml:
<?php
$str = <<<XML
<currencies>
<currency name="US dollar" code_alpha="USD" code_numeric="840" />
<currency name="Euro" code_alpha="EUR" code_numeric="978" />
</currencies>
XML;
$currencies = new SimpleXMLElement($str);
foreach($currencies as $currency)
{
echo $currency['name'], "\n";
echo $currency['code_alpha'], "\n";
echo $currency['code_numeric'], "\n";
echo "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n";
}
?>
Live DEMO.
Related Topics
How to Check If a Value Already Exists to Avoid Duplicates
How to Capture Search Term from Google Search
JSON: PHP to JavaScript Safe or Not
Php, MySQL - Too Many Connections to Database Error
Jquery Load() and PHP Variables
Find Multiples of a Number in PHP
How to Strip Out the Domain Name from a Url in PHP
Yii2 Require All Controller and Action to Login
How to Create Thumbnails or Preview for Videos
How to Use PHPunit to Test a Function If That Function Is Supposed to Kill PHP
$_File Upload Large File Gives Error 1 Even Though Upload_Max_Size Is Bigger Than the File Size
How to Eliminate PHP5 Strict Standards Errors
How to Retrieve the Visitor's Isp Through PHP
Upload Multiple Files to PHP Server Using Curl Command Line
Wordpress Woocommerce - Use Update_Post_Meta to Add Product Attributes