views:

245

answers:

3

I have a PHP script on a server that generates the XML data on the fly, say with Content-Disposition:attachment or with simple echo, doesn't matter. I'll name this file www.something.com/myOwnScript.php

On another server, in another PHP script I want to be able to get this file (to avoid "saving file to disk") as a string (using the path www.something.com/myOwnScript.php) and then manipulate XML data that the script generates.

Is this possible without using web services? security implications?

Thanks

+7  A: 

Simple answer, yes:

$output = file_get_contents('http://www.something.com/myOwnScript.php');

echo '<pre>';
print_r($output);
echo '</pre>';
Alix Axel
+4  A: 

If you want more control over how you request the data (spoof headers, send post fields etc.) you should look into cURL. link text

Mike Stanford
curl is more flexible and performant, but file_get_contents() is simpler.Personally, I tend to start with file_get_contents(), until curl's power/performance become necessary.
Frank Farmer
cUrl allow even the HTTP authentication, really usefull (of course, just when is needed)
DaNieL
+1  A: 

If you're on a shared host, you might find that you cannot use file_get_contents. This mainly because it is part of the same permission sets that allow you to include remote files. Anyway...

If you're stuck in that circumstance, you might be able to use CURL:

<?php
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "example.com");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);     
?>

It is more code, but it's still simple. You have the added benefit of being able to post data, set headers, cookies... anything you could do with a highly configurable browser. This makes it useful when people attempt to block bots.

Oli