I'm trying to use glob() to get all of the XML files from an external URL, but I'm not getting any results back
foreach(glob("http://www.externalsite.com/files/*xml") as $filename) {
$xml_file = file_get_contents($filename, FILE_TEXT);
echo $filename;
}
Is this possible? Or is there another method of doing this?
maybe someone can help me, i provide xml files witch are generated from a PHP DB query and each xml file has a unique name. Now i want to prepare a function like "get the latest xml file" but I don't know whats the best way!
$xml = simplexml_load_file('test.xml');
I found this function but there i have to know the exact name!
or ist something like this possible:
$xml = simplexml_load_file('test.php');
and in the test.php i have a function to get the last name, but how to i provide the xml data?
Some keywords how i can find a solution in google would be very helpful!
The first parameter to that function is a string of the filename. The file should be the XML file to load, so you cant use another php file.
http://php.net/manual/en/function.simplexml-load-file.php
So you need to get the filename as a string first by using a variable. You should be able to copy the code in your test.php file, then save the filename instead of echoing it out. Then you use that variable when loading the xml file.
e.g.
function get_latest_filename()
{
//contents of your test.php file should set this variable
$latest_filename = 'the_latest_file.xml';
return $latest_filename;
}
$latest = get_latest_filename();
$xml = simplexml_load_file($latest);
here the finish solution that worked for me
i protected the directory with .htaccess and inside i store all my generated xml files and also the getLastXml.php file!
the getLastXml.php
function get_last_file() {
$lastFileTime = 0;
foreach (glob("*.xml") as $filename) {
if ($lastFileTime<filemtime($filename))
{
$lastFileTime = filemtime($filename);
$lastFileName = $filename;
}
}
return $lastFileName;
}
$lastXmlFile = get_last_file();
header ("Content-Type:text/xml");
echo file_get_contents($lastXmlFile);
the functions get_last_file() returns the name of the latest created xml file and
header ("Content-Type:text/xml");
displays xml in the php file
echo file_get_contents($lastXmlFile);
loads the content of the xml file and display it
simplexml_load_file("http://username:passwort#urlToTheDirectory/getLastXml.php");
loads the xml data with
I'm trying to get a few PDF files I need for a project from a website (legally), but am running into some issues.
The URL of the location of the PDF file is f.e. example.com/?download_id=290758&s=d12134cac7ddb2198d232bba75c07d57&t=2-1-2014%2016:19:00
So first of all I parse this URL from the page containing it with the following code:
$html = file_get_html('http://www.example.com/Acer-CR-6530/manual-5-100076.html');
// find the download link containing the session ID
foreach($html->find('a[rel=nofollow]') as $e)
$link = $e->href;
$link = $baseURL . $link;
// Link: example.com/?download_id=290758&s=d12134cac7ddb2198d232bba75c07d57&t=2-1-2014%2016:19:00
echo file_get_contents($link);
This doesn't work. It doesn't output the PDF file. Should I use cURL? If so, I'm not a cURL pro so I would love to see code for doing this.
Thanks in advance!
I'm trying to get the below code to work. Currently it just outputs nothing, a blank page.
if($vUrlDetails = simplexml_load_file($vUrl)) {
// Do something
}
It works fine if I use file_get_contents() and save it to a file and then use simplexml_load_file().
Are there restrictions on loading XML files over a URL using simplexml_load_file()?
I think you can try:
<?php
// http://www.php.net/manual/en/simplexmlelement.construct.php
$xml = new SimpleXMLElement('http://my.url.com/something.xml', 0, true);
?>
I have been working on a project that displays data in an XML file. (It's kind of like an API). I know how to parse XML with PHP, and how to make an XML file in PHP, but they don't work together. :)
Basically, I have two files: parse.php and xml.php.
xml.php grabs info from a MySQL database, and outputs it as XML.
parse.php loads and parses xml.php and outputs it as HTML.
If I run parse.php, it does not load xml.php. However, if I copy the outputted XML (from xml.php) and save it as a xml.xml file (and change the filename in parse.php to 'xml.xml') it works. I'd really appreciate any help.
Content of parse.php:
<?php
$doc = "xml.php";
$doc = #simplexml_load_file($doc) or die("Server Error: Recipe not found!");
$title = $doc->title;
echo $title
?>
Content of xml.php:
<?php
header("Content-type: text/xml");
$dbc = mysql... //gets data from database
echo "<!DOCTYPE..."; //xml stuff here
echo "<title>" . $dataFromMySQL . "</title>";
?>
The database connection works, and the DOCTYPE in the XML is ok, so that's not the problem.
Again, I only get the problem when I generate XML dynamically using PHP. If it's a .XML file, it works fine.
Can anyone help me?
Thanks.
simplexml_load_file will try to actually load the php contents of the xml.php file. It will not run that file first. You need to do some rewriting or use this ugly solution:
ob_start();
include 'xml.php';
$xml = ob_get_clean();
$doc = simplexml_load_string($xml);
//...
NOTE: I like #lonesomeday's proposed solution better, it will just require more rewriting.
#simplexml_load_file($doc);
That is where your problem is. This does not execute xml.php, but attempts to parse that file -- the PHP code that you've written -- as XML. Obviously (since it isn't XML) this won't work.
You have to find a way of getting the output from executing xml.php into parse.php.
The easy way to do this would be to change all your echo calls into $xml .= calls, and simply include xml.php into parse.php.
// xml.php
$xml = '';
$xml .= "<!DOCTYPE..."; //xml stuff here
$output .= "<title>" . $dataFromMySQL . "</title>";
// parse.php
include('xml.php');
simplexml_load_string($xml);
Note that your problem here shows the foolishness of using the error suppression operator #. If you hadn't used it, PHP would have shown you various errors which would have helped you to realise what the problem was.
Addendum: it occurs to me that the best way actually is to forget about the pointless XML step along the way and just convert the database output into HTML.
If you want to do this without rewriting xml.php, you can get PHP to process the file by accessing via url:
$doc = file_get_contents("http://localhost/xml.php");
You're literally loading the local file. Unless you evaluate it, the code doesn't run, so you'll just get the code itself.
You could use CURL to download xml.php over HTTP, or you could make the XML-generation component of the xml.php a callable function which you simply include and execute.
parse.php:
<?php
include('xml.inc');
$doc = #simplexml_load_string(xml_function()) or die("Error");
echo $doc->title;
xml.php:
<?php
include('xml.inc');
header("Content-type: text/xml");
echo xml_function();
xml.inc:
<?php
function xml_function() {
$dbc = mysql... //gets data from database
$xml = "<!DOCTYPE..."; //xml stuff here
$xml .= "<title>" . $dataFromMySQL . "</title>";
return $xml;
}
But... even that seems silly, honestly, when you could have both output methods connect to the same data and skip a generation/parse step. Simply output HTML/XML conditionally.