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.
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
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']);
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!
Related
I have been given an example piece of code from a company I'm dealing with for how to post XML data to a URL then read the response. Unfortunately for me this in VBS which I don't have a good working knowledge of:
This is the section of code that I'm interested in. This should pass over the XML file that was read in to oXML then post it and read the response:
set oHTTP = CreateObject("Microsoft.XMLHTTP")
oHTTP.open "POST", "http://www.ophub.net/opxml/response.asp", false,00092,QW 'file url - with dealers Account number, Password
oHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
oHTTP.setRequestHeader "Content-Length", Len(sRequest)
oHTTP.send oXML
From what I understand of this in PHP this can be done with cUrl and I have come up with the following from bits that I have read online but this doesn't work and I'm not sure why.
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form- urlencoded"));
curl_setopt($ch, CURLOPT_URL, "http://www.ophub.net/opxml/response.asp");
curl_setopt($ch, CURLOPT_USERPWD, "00092:QW");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "XML=" . $xml);
$content=curl_exec($ch);
echo $content;
I'm sure I can't be far off what I need but I can't seem to get there so any hep would be very much appreciated.
In your post fields just set the xml no need to set xml=
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
you also need return transfer true
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
It looks to me like that is precisely what you need. It seems to be an exact translation.
The only two potential issues that I can see are:
You PHP code seems to have a space in the middle of the Content-Type header value, this will most likely break things
You will, if you haven't already done so, need to URL encode the $xml data before sending it as part of a application/x-www-form-urlencoded message (clue's in the type name :-P), which you can do with urlencode().
It might be a good idea to build the body data as a string and echo it out, to ensure that the data is correct:
echo 'XML=' . urlencode($xml);
For the record wrapping XML messages in application/x-www-form-urlencoded is a horrible way to do things. But I've come across more than one API that does it, so I'm going to assume that your code is correct in this regard for the API you are trying to consume.
I've been developing an "interface" were some client of mine, can call an API passing to it an XML structure and receive back another XML.
I've been create routes,methods and so on and try to use cURL for posting my data: everything works like a charm.
BUT
.... if I add Content-Type: text/xml; charset=utf-8 to request's header, suddenly, in POST variable, there isn't anything anymore.
Any idea of what is going on here?
Note
Obviously I don't add any piece of code, it's very simple and is composed by a route (standard symfony2 route), a method (dump POST) and the call that contain xml.
This is code of call
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'xxxxxxx:yyyyyy'); //where xxxxxx:yyyyy is setted but now censored
curl_setopt($ch, CURLOPT_URL, $this->url); // set url to post to
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml; charset=utf-8")); //without this all works
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->message);
where $message is something like this (just an example)
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
<Message Option="1">Option 1</Message>
</ROOT>
After some days of debug, headache and so on, I found something that make my day, but that sounds weird to me.
If I log the incoming request, nothings seem to be posted to me (even if I post some xml).
BUT if i add $logger->debug(__CLASS__.' '.$this->get("request")->getContent()); before log all incoming request, next request will be logged with all parameters that I'd post.
This sound like a kind of "lazy loading". So, by calling $this->get("request")->getContent() into my controller, I can get what I'm asking for (xml posted parameters)
Can someone help me understand?
I am trying to use the DELL API to fetch warranty information from our products. DELL provides an XML file that I want to extract the end of warranty date from.
Location of the XML file: https://api.dell.com/support/v2/assetinfo/warranty/tags?svctags=XXXXXX1&apikey=YYYYYYYYYYYYYYYYY
[Note: svctags and apikey obfuscated]
Code:
$xml = simplexml_load_file("https://api.dell.com/support/v2/assetinfo/warranty/tags?svctags=XXXXXXX1&apikey=YYYYYYYYYYYYYYYYYY");
print_r($xml->GetAssetWarrantyResult->{'a:Response'}->{'a:DellAsset'}->{'a:Warranties'}->{'a:Warranty'}->{'a:EndDate'});
This is not working. I get an empty page when executing this code. Can anybody explain why? Thanks in advance!
you can used curl, For ex:
$url = "https://api.dell.com/support/v2/assetinfo/warranty/tags?svctags=1RP22W1&apikey=1adecee8a60444738f280aad1cd87d0e"
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
$response = curl_exec($ch);
In case you wanted more.
get url: https://api.dell.com/support/v2/assetinfo/warranty/tags.json?svctags="
+tag+"&apikey=1adecee8a60444738f280aad1cd87d0e
Then parse the response in (javascript).
In python you could also read each line page.readlines() and use encode=json.loads(data) to start reading the result like a dictionary.
I am new to PHP, and I am using it to POST data from an iPhone. I have written a basic code to get data from the iPhone, post it to the php script on my server, and then using PHP, send that data to another webserver. However, the webserver is returning a response in XML, and since I am a newbie to PHP, I need help with it.
My code to send data:
<?php
$ch = curl_init("http://api.online-convert.com/queue-insert");
$count = $_POST['count'];
$request["queue"] = file_get_contents($count);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($ch);
curl_close ($ch);
echo $response;
?>
I know I need to parse the XML response, but I have no idea how to do that. The XML response would be something like this:
<?xml version="1.0" encoding="utf-8"?>
<queue-answer>
<status>
<code>0</code>
<message>Successfully inserted job into queue.</message>
</status>
<params>
<downloadUrl>http://www.online-convert.com/result/07d6c1491bb5929acd71c531122d2906</downloadUrl>
<hash>07d6c1491bb5929acd71c531122d2906</hash>
</params>
</queue-answer>
You're probably looking for SimpleXML or DOMDocument.
SimpleXML
To load data from a string into an object: simplexml_load_string().
DOMDocument
To create one from a string: DOMDocument->loadXML().
Read about SimpleXML: http://php.net/manual/en/book.simplexml.php
Google is your friend!
The built-in PHP XML Parser is your best bet.
I am writing a test harness to assist in testing a method in a PHP class that I have. Essentially, that method sends XML data as a POST request to the LinkedIn API, so for testing purposes, my test harness will act as the mock LinkedIn API endpoint, taking the posted data and returning various responses to see how the method handles them.
So, the method uses cUrl to send data like so:
$handle = curl_init()
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($handle, CURLOPT_URL, 'http://localhost/target.php');
curl_setopt($handle, CURLOPT_VERBOSE, FALSE);
$header[] = 'Content-Type: text/xml; charset=UTF-8';
curl_setopt($handle, CURLOPT_HTTPHEADER, $header);
$data = '<?xml version="1.0" encoding="UTF-8"?>
<activity locale="en_US">
<content-type>linkedin-html</content-type>
<body>Network update</body>
</activity>';
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($handle);
Now, on my target.php script, I'd like to access the POSTed data... but when sending non key=value paired data, $_POST is always empty. If I omit the custom header and replace $data with $data = 'key=value', $_POST['key'] contains value. Keep in mind that this method works in terms of sending the XML data as above and the LinkedIn API responding.
So, any pointers on how to access POSTed data that is sent in non-key value pair format and possible a custom header type?
Looks like the correct solution is to access the raw POST data via:
<?php $postdata = file_get_contents("php://input"); ?>
Per http://php.net/manual/en/reserved.variables.httprawpostdata.php
It is called raw post data:
$xml = $HTTP_RAW_POST_DATA;