Reputation: 577
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.
Upvotes: 0
Views: 1028
Reputation: 577
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!
Upvotes: 0
Reputation: 2982
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']);
Upvotes: 1
Reputation: 3560
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
Upvotes: 1