SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

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

  2. #2
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    363
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    My pseudo-code would go something like:

    Code:
    $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.
    http://www.firenza.net - my homage to a car from the 1970s

  3. #3
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,389
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hey stowman,

    Something like this would do the trick:

    PHP Code:
    <?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_filenamefile_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>

  4. #4
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  5. #5
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,389
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Try adding the following as the first line of PHP code, to make sure that any errors are displayed:

    PHP Code:
    error_reporting(E_ALL E_STRICT); 
    You should also check your server's PHP error log to see if it's complaining about anything.

  6. #6
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  7. #7
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,389
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by stowman View Post
    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
    Replace the html section of the file with this:

    PHP Code:
    <!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>

  8. #8
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you again, fretburner. The changed HTML code resulted in the following :

    screenshot.jpg

    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/



    Any thoughts?

  9. #9
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,389
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi stowman,

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

    PHP Code:
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •