Hi,

I have a help desk app that runs in a browser. It can generate XML code which is a report, for example, of current open tickets. To do this it produces a URL like this:

http://servicedesk.schoolname.internal:8081/helpdesk/WebObjects/Helpdesk.woa/wa/TicketQueryChartActions/xmlData?widget=59&username=john&password=testpw

Note: It needs the username & password at the end so it can load ok if you're not actually logged into the app.

This URL produces the XML within the browser and you then have to save the source to an actual XML file manually.

I want to automate this so I can use the XML file to display a live dashboard monitor so our help desk staff can see, from a distance on a big screen, how tickets are doing.

So my question is, how can I save the XML output from that URL to a file using PHP?

I've Googled and searched this forum to no avail so far. Any help would be really appreciated.

Thanks,

John

    Thanks bradgrafelman.

    I've tried this code which works perfectly when reading from a simple test HTML page on my local host:

    <?php
    $file = 'filename.xml';
    $source = file_get_contents("http://localhost/clock/testpage.html");
    file_put_contents($file, $source);
    echo $source;
    ?>

    However, when I substitute the url for the one I mentioned in my first post, the one that generates the XML, I get a blank page and an empty file.

    Could it be that as the URL from the help desk app takes a second or so to do the search and load the page, that the PHP is missing it? Or as it's not actually generating a file as such, this is the wrong approach?

    Again, any help much appreciated.

    Thanks, John

      mazetrix;11029055 wrote:

      Could it be that as the URL from the help desk app takes a second or so to do the search and load the page, that the PHP is missing it?

      Not sure what you mean by "missing it." If PHP timed out the request and gave up, it would emit an error message; it wouldn't silently pretend that the request succeeded but the remote server simply returned no content.

      Do you have display_errors set to On and error_reporting set to E_ALL (or better)?

      mazetrix;11029055 wrote:

      Or as it's not actually generating a file as such, this is the wrong approach?

      Again, not sure what you mean here. You previously said that a file was created but that it was empty. So, which is it?

        Ok, turned on display_errors (thanks for the pointer there) and I get this when testing with the help desk app URL:

        "( ! ) SCREAM: Error suppression ignored for
        ( ! ) Warning: file_get_contents(http://servicedesk.school.internal:8081/helpdesk/WebObjects/Helpdesk.woa/wa/TicketQueryChartActions/xmlData?widget=59&amp;username=john&amp;password=testpw): failed to open stream: Redirection limit reached, aborting in C:\wamp\www\clock\whd2works2.php on line 3"

        Further testing and it seems I get errors on 'any' URL that isn't my localhost. Here's an example:

        "( ! ) SCREAM: Error suppression ignored for
        ( ! ) Warning: file_get_contents(http://www.yourmovecreep.co.uk/testpage.html): failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\wamp\www\clock\whd2works2.php on line 3"

        On the generating file point, I meant the file that my PHP code creates (filename.xml) is empty. The help desk app URL doesn't create a file, it just outputs the XML code to the browser window.

        So from the errors above, any thoughts? Is it because I'm using a WAMP server localhost?

        Thanks, John

          mazetrix;11029059 wrote:

          Further testing and it seems I get errors on 'any' URL that isn't my localhost.

          Sounds like you've got some sort of firewall/proxy issue that's preventing PHP from connecting to an external server. For example, if you can successfully browse to these sites using a web browser on the same PC, perhaps your browser is configured to use an internal proxy.

          mazetrix;11029059 wrote:

          The help desk app URL doesn't create a file, it just outputs the XML code to the browser window.

          Well... that's because no URL can "create a file" since that doesn't really make any sense. A URL is just a way for you to ask a remote server to give you the data corresponding to a certain resource; URLs have no direct relation to files.

            Maybe saying URL was wrong terminology. I guess I meant the help desk app doesn't create a file, just a URL which displays the XML.

            Could well be a firewall issue actually, yes. We have Smoothwall running blocking sites as well. Can I add proxy server details to the PHP code to get around it?

            Thanks, John

              mazetrix;11029065 wrote:

              Maybe saying URL was wrong terminology. I guess I meant the help desk app doesn't create a file, just a URL which displays the XML.

              Right, but the end result is the same: your browser (and/or PHP) can't tell where the data is coming from (be it a file or a server-side script).

              mazetrix;11029065 wrote:

              Can I add proxy server details to the PHP code to get around it?

              One way would be to use [man]cURL[/man] instead; see the manual page for [man]curl_setopt/man (just search for "PROXY" to find the relevant options).

                You might find it helpful in the future to check the results of file_get_contents before blithely assuming everything worked:

                <?php
                $file = 'filename.xml';
                $source = file_get_contents("http://localhost/clock/testpage.html");
                if ($source === FALSE) { // file_get_contents returns FALSE when it fails
                  die("Fetch result failed!");
                }
                file_put_contents($file, $source);
                echo $source;
                ?>
                

                  Thanks for all the advice bradgrafelman, I'll check out cURL.

                  Thanks for the extra code sneakyimp. I'm new to this, hence seeking help and guidance, so more unaware of all the tips and tricks as yet rather than being blithely, but I understand your sentiment ;-)

                  As long as I'm on the right course with this idea was my main concern, but it looks like cURL might hold the answers.

                  Thanks all, John

                    17 days later
                    Write a Reply...