How to capture an XML file from URL?

Hello All. Can anybody help me create an index.php file which does the following:

I have the following URL:

http://api.vcwhitelabel.co.uk/[filename].xml?partner=personality&auth=52d4f588d4ec3dabeabc82861fb3f8bb&show=1000

which returns an XML file which is generated by an online database.

The parameter [filename] can have one of four values:
retailers
categories
subcategories
discounts

The “&show=” parameter determines the number of records to be included.

= = = = = = = = = = = = = =
What I need is a way to capture this XML file so that I can download it to my PC.

I also need to be able to”
(1) select between which of the 4 [filename] parameters is submitted, and also
(2) adjust the value of the “&show=” parameter between 1 and 9999999999.

The name of each saved XML file should be in a format similar to the following:

[filename]-[timestamp]-[&show]

where
[filename] is one of the four words given above
[timestamp] is the date/time at time of capture
[&show] is the value of “&show=” submitted

The timestamp can be a string such as 2013-10-23-1125 (meaning 23rd October 2013 at 11:25am) or any other meaningful timestamp if that is easier.

For example, a file might be called:
discounts-2013-10-23-1125-999.xml

= = = = = = = = = = = =

During testing the “&show=” value must be restricted to a small value such as 10 or 20.

= = = = = = = = = = = =

I have FTP access to my own domain, which is running PHP5, and so it would be sufficient for the PHP code to be in an index.php file which saves all the XML files into the same folder as itself, from where I can download them at my leisure.

= = = = = = = = = = = = =

My pseudo-code would go something like:


$showqty = 100; // change as required
for $param = "retailers", "categories", "subcategories", "discounts";
$url = $start-of-url . $param . $end-of-url . $showqty;
$xmldata = file_get_contents($url);
if ($xmldata) {
   file_put_contents($my-local-filename, $xmldata);
   }
}

I suspect there’s a better way to handle each filename prefix, using an array and foreach(), but I haven’t done much with that. Then just a case of setting the local filename and saving the data. Probably.

Hey stowman,

Something like this would do the trick:


<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $save_path = __DIR__;

    $filename = $_POST['filename'];
    $datetime = date("Y-m-d-Hi");
    $show     = $_POST['show'];

    $local_filename = "$save_path/$filename-$datetime-$show.xml";
    $url = "http://api.vcwhitelabel.co.uk/$filename.xml?partner=personality&auth=52d4f588d4ec3dabeabc82861fb3f8bb&show=$show";

    file_put_contents($local_filename, file_get_contents($url));
}

?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form method="post">
        Number of records to download <input type="text" name="show" value="20">
        Filename <select name="filename">
            <option value="retailers">retailers</value>
            <option value="categories">categories</value>
            <option value="subcategories">subcategories</value>
            <option value="discounts">discounts</value>
        </select>
        <input type="submit">
    </form>
</body>
</html>

Hello fretburner. (I sent a reply a short while ago, but my PC then crashed, and I cannot see my reply in this thread, so I’ll repeat it now in case it didn’t get sent).

I copied the PHP code you kindly gave me into a file and named it index.php
I then uploaded it to a newly-created folder on my Linux server runing PHP5.
When I visit that folder (www.ezshopuk.com/petoba1) I see what I would expect (a field to enter Number of records, a dropdown to select one of 4 options, and a Submit button).

But when I click the Submit button, nothing seems to happen. And no file appears in the same folder as the index.php file.

Can you help me with this? Am I looking in the correct place?

Try adding the following as the first line of PHP code, to make sure that any errors are displayed:

error_reporting(E_ALL | E_STRICT);

You should also check your server’s PHP error log to see if it’s complaining about anything.

Hi again fretburner. No luck I’m afraid. Adding the error_reporting line to the code didn’t produce any error reporting onscreen, and I haven’t been able to find a way to view my server’s error logs.

Could you possibly add a “success” page which shows:
(1) the URL as constructed (so I can copy it and try it manually);
(2) the filename

Many thanks. stowman

Replace the html section of the file with this:


<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style>
        .results { border: 2px solid lightgreen; margin-bottom: 10px; }
    </style>
</head>
<body>
    <?php if (isset($local_filename) && isset($url)): ?>
    <div class="results">
        <ul>
            <li>URL: <?php echo $url ?></li>
            <li>Saved to: <?php echo $local_filename ?></li>
        </ul>
    </div>
    <?php endif ?>
    <form method="post">
        Number of records to download <input type="text" name="show" value="20">
        Filename <select name="filename">
            <option value="retailers">retailers</value>
            <option value="categories">categories</value>
            <option value="subcategories">subcategories</value>
            <option value="discounts">discounts</value>
        </select>
        <input type="submit">
    </form>
</body>
</html>

Thank you again, fretburner. The changed HTML code resulted in the following :

which all looks correct – I can copy-and-paste the URL string it contains into my browser, and get the expected result – but no file appears in the folder /httpdocs/petoba1/

:frowning:

Any thoughts?

Hi stowman,

Try adding this line to your PHP, just before the file_put_contents line:

echo file_get_contents($url); 

If the script is getting the data back from the remote server OK, then it will be output at the top of the screen.