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.

3
  • you would be better using curl and post the data to the field. see here stackoverflow.com/questions/2138527/… Commented Jan 5, 2014 at 13:38
  • @LiamSorsby I totally agree with you, but in my case I absolutely mustn't change output.php so that is not a solution in my case. Commented Jan 5, 2014 at 13:41
  • See my answer below. It may help Commented Jan 5, 2014 at 13:42

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

6 Comments

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?
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
@Janisimo, I have edited my answer so it has got an example of usage.
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?
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.
|
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']);

3 Comments

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.
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'];
@Janisimo i'm just looking into it now
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.