-2

I have this in file called abc.php, and this will return a valid xml document, instead of showing -string- labels at the end and beggining

header('Content-type: application/xml');
$xml = file_get_contents("http://www.xxx.asmx/test?id=1"); //External web service
$xmlstr = simplexml_load_string($xml);
echo $xmlstr;

I want to use the valid xml data of abc.php, extract certain data, store it in my db, and check the output of the other server periodically, I've tried this:

ob_start();
include 'abc.php';
$result = ob_get_clean()

as well as this:

$xml = file_get_contents("abc.php"); 
$xmlstr = simplexml_load_string($xml);

without success, any advice?

Christian Stewart
  • 15,217
  • 20
  • 82
  • 139
  • 1
    What's wrong with your code? – Christian Stewart Mar 01 '13 at 18:19
  • this is abc.php [link](http://sanjosecostarica.org/test/abc.php) this is the code $result = file_get_contents('abc.php'); var_dump ($result) and this is the result [link](http://sanjosecostarica.org/test/aaa.php); if i use a local xml it will display the content right but not if i call the php – evil kiwi Mar 01 '13 at 18:31
  • It appears to be returning valid XML. I was asking what was wrong with your code to parse the xml? Are you asking us to tell you how to parse it and put it in the database? – Christian Stewart Mar 01 '13 at 18:32
  • I'm actually having problems when I try to get contents of "abc.php" to parse it in another php "aaa.php" links above – evil kiwi Mar 01 '13 at 19:23
  • 1
    possible duplicate of [PHP HttpRequest](http://stackoverflow.com/questions/301637/php-httprequest) or [Execute a PHP file, and return the result as a string](http://stackoverflow.com/questions/1683771/execute-a-php-file-and-return-the-result-as-a-string) – hakre Mar 02 '13 at 11:45

3 Answers3

0

Make sure you output the MIME Type as well, or else the server will feed text/html to it and it will be all wrong. Put this function

header("Content-type: application/xml");

in abc.php so the client will recognize it as XML.

tyteen4a03
  • 1,812
  • 24
  • 45
0

file_get_contents("abc.php") will return you the contents of the file "abc.php"; it will not execute that PHP code. The include with output buffering trick ought to do roughly the right thing, but I'm not sure why you'd ever want to do it that way, so it's not worth working out why that's failing.

If you can access the code in abc.php, then simply make it into a PHP function, which returns the processed XML:

function get_the_actual_xml()
{
    $xml = file_get_contents("http://www.xxx.asmx/test?id=1"); // External web service
    $xml_obj = simplexml_load_string($xml); // Load into SimpleXML object
    return (string)$xml_obj; // Convert contents back to a string
}

If for some reason your two PHP files need to be on different servers, you will need to reference the URL to abc.php, not just where it is on disk. That way, the PHP code will be executed, and what you'll get back is the result of that echo statement. If your server has the allow_url_fopen setting enabled, this is as simple as $remotely_processed_content = file_get_contents('http://sanjosecostarica.org/test/abc.php')

IMSoP
  • 89,526
  • 13
  • 117
  • 169
0

at the end I couln't get the results of "abc.php" but only the content, I try a different approach successfully:

$xml = file_get_contents("http://www.xxx.asmx/test?id=1"); //External web service
$xmlstr = simplexml_load_string($xml);
$xmlok = <<<XML
$xmlstr
XML;

$xml = simplexml_load_string($xmlok);