0

Thank you all, I have solved this and wrote my own answer on this question down below. Thank you all for trying


I have a simple (I hope) question as I am a complete newbie.

So I have 2 php files output.php and input.php.

output.php is sending an XML to input.php via cURL like so:

$myXML  = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
$myXML .= '<request>
               <message>Hello StackOverflow</message>
               <params>
                   <name>John</name>
                   <lname>Smith</lname>
               </params>
           </request>';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://localhost/curltest/input.php');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $myXML);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
    array(
        'Content-type: text/xml; charset=UTF-8',
        'Expect: '
    )
);

$curl_response= curl_exec($curl);

Currently the contents of input.php look like this

<?php
// Yes, this is all

The problem is I don't know at all how to get and parse the XML data in input.php and take data out of that XML for use in my input.php script.

$_POST is an empty array in input.php, but the XML surely reaches input.php because readfile('php://input'); inside of input.php gives out the received XML.

I have read about DOMElement but it was kind of unclear to me how to use it in my specific case. I also did a search on Google and in SO also, but didn't succeed in finding exactly what I need.

If someone could tell me how exactly is this done it would be great. Or at least point me in the right direction, I'm at a loss here.

P.S. I can't change anything in output.php. And I will gladly provide any additional information if needed.

Janis Vepris
  • 577
  • 3
  • 13

3 Answers3

1

Simple XML is a really good library for XML parsing/reading/writing.

http://www.php.net/manual/en/book.simplexml.php

PHP:

$file = new SimpleXMLElement($file);
echo "<b>Testing:</b> {$file->testing} <br/>";

XML:

<document>
    <testing>Value</testing>
</document>

You can use Simple XML both from files and variables.

You can find more information about it here: http://www.php.net/manual/en/simplexmlelement.construct.php

GiamPy
  • 3,240
  • 1
  • 25
  • 45
  • Thanks! I've read on it too, but in this case I have another problem, I don't know how to load the XML string that is sent into the SimpleXML in input.php Any suggestions? – Janis Vepris Jan 05 '14 at 13:35
  • Please note depending on the size of the XML file simple XML is okay for files up to the size of about 10mb as simplexml loads everything to memory, if you load anything more then 10mb you maybe better with XMLreader – Liam Sorsby Jan 05 '14 at 13:36
  • @Janisimo, I have edited my answer so it has got an example of usage. – GiamPy Jan 05 '14 at 13:38
  • Thank you so much for trying to help, but my problem is that the XML is not in a separate file, it's sent to input.php as shown in my question, so how do I load it into SimpleXML? – Janis Vepris Jan 05 '14 at 13:45
  • In your question you set $myXML as post field, @Janisimo. I am not expert with curl, but you may try using `$_POST['myXML']` or try `var_dump($_POST)` as @LiamSorsby said so you can see if the variable has been set properly. – GiamPy Jan 05 '14 at 13:46
  • @Janisimo, try using `var_dump($_POST)` and see if what you're looking for is there. – GiamPy Jan 05 '14 at 13:50
1

you need to change

curl_setopt($ch, CURLOPT_POSTFIELDS, $myXML);

to

curl_setopt($ch, CURLOPT_POSTFIELDS,  http_build_query(array('xml' => $myXML)));

then in your input.php file you can get the data using $_POST['xml'];

so in your input.php then do

$xmldata = new SimpleXMLElement($_POST['xml']);
Liam Sorsby
  • 2,634
  • 2
  • 24
  • 49
  • You are awesome, I have thought about this but unfortunately I cannot change absolutely anything in output.php unfortunately. This is a university assignment, so that's a problem. – Janis Vepris Jan 05 '14 at 13:46
  • right as you are posting the data the if in your input.php if you var_dump($_POST); then it will tell you the POST variable name as it hasn't been set (i imagine it is going to be something like $_POST[0]). Once you have var dumped it you should be able to change $_POST['xml'] to $_POST[0] or $_POST['Some_Name']; – Liam Sorsby Jan 05 '14 at 13:48
  • @Janisimo i'm just looking into it now – Liam Sorsby Jan 05 '14 at 14:11
0

I've managed to solve it by getting the sent XML string into a variable in the input.php like so:

$xml_data = new SimpleXMLElement(file_get_contents('php://input'));
var_dump($xml_data);

Now I may do whatever I want with it. Thank you all for trying, you're all amazing!

Janis Vepris
  • 577
  • 3
  • 13