How do I receive XML data via a webhook in PHP? - php

I've been using StackExchange for many years but I have a problem I haven't found any answers for elsewhere and I'm hoping for some help.
The PHP program I'm working on is trying to receive an XML from a webhook, take that data, convert it via XSLT, and send via cURL POST the new XML to a third party.
The conversion part works great, but I'm having trouble receiving the data. If I just read from a local XML file, that's all dandy, but if I try to use php://input, the end result is just an empty XML sent to the third party.
Here's the relevant-code snippet: I can give the other stuff if it's any help!
//initialize the xml for the XSLT work
$xml = new DOMDocument;
//the part of the code looking for the data
$input = file_get_contents('php://input');
$xml = simplexml_load_string($input);
//the XSLT stuff is hereout
$xsl = new DOMDocument;
$xsl->load('ConversionSheet.xsl');
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
$XSLTout = $proc->transformToXML($xml);
I'm using XAMPP to host the PHP and go to https://localhost/projectname/filename.php/ in Firefox.

Related

How to get content of any file that is having authentication with it in php?

I am reading one xml file from my uploads folder with file_get_contents and its working for me.
Now i want to read xml file from another server that is having authentication to read file.
so, how can i read that file. please help me .
My current code to read xml file :
$xml = file_get_contents('uploads/data.xml');
$xml = simplexml_load_string($xml);
$xml_array = json_decode(json_encode((array) $xml), 1);
I am having one url and Username/Password to get xml data from it.
I want something like this but as this comes with authentication part am not able to read file.
$xml = file_get_contents('https:dummyurl/feed.xml'); //some url and xml file.
$xml = simplexml_load_string($xml);
$xml_array = json_decode(json_encode((array) $xml), 1);
Unfortunately you can not authenticate using file_get_contents, to retrieve a response from a server which requires authentication you will need to use cURL. Depending on the authentication type, you may need to send specific headers, or in some cases even make separate requests (one to authenticate and retrieve some token, and then one to request resource with that token).

Send XML to Soap Web service php

I am really new to Web Services and xml and I'am Having problems sending xml to a web server
I' am using SoapCliente Php class
$client = new SoapClient($wsdlUrl, $soapClientOptions);
I'm not having any problem sending strings
$param = ["key" => "somekey"];
var_dump($client->somemethod($parms));
But, I am having probles sending xml data as is requered on the web service documentation
#WebParam(name = "xml") byte[] xml);
On the getTypes method:
var_dump($client->__getTypes());
Is specified in this way:
base64Binary xml
So I first stored the xml file as a string variable and then i tried:
$xmlstring = "some xml";
$xmltosend = unpack('C*', $xmlstring);
var_dump($client->somemethod($xmltosend));
And:
$xmlstring = "some xml";
$xmltosend = base64_encode($xmlstring);
var_dump($client->somemethod($xmltosend));
The Web Service always send a response acording to the data so if the send was correct i will get some succes xml data. In both cases I get this error:
FILES SENT NOT COMPLY WITH THE ESTABLISHED SPECIFICATIONS: EXTENSION, CODIFICATION
The xml format that i am sending is provided as a test example by the web service's documentation so i think the xml is Ok, so I am clueless of what I am missing.
Edit
This is exactly how the params are excected on the documentation
#WebMethod
#WebResult(name = "RespuestaRecepcionComprobante")
public RespuestaSolicitud validarComprobante(#WebParam(name = "xml") byte[]
xml);
So i just sended the xml string and it worked
$param = ["xml" => $xmlstring];
var_dump($client->validarComprobante($param));
It's ok to send the data like this?
The web service read the xml and send me an Ok response
I also expect an xml response but I dont know how to catch it

SimpleXMLElement:difference in Curl and file_get_contents

So guys, there is another question.
I've fetched a webpage via curl and constructed XML document:
.......
$data = curl_exec($ch1);
$xml = new SimpleXMLElement($data);
And it worked correctly.
After I used file_get_contents like this
......
$file1 = file_get_contents('https://meet77842937.adobeconnect.com/api/xml?action=report-my-meetings', false, $context);
$xml = new SimpleXMLElement($file1);
The last code cannot construct XML, though I able to see through var_dump that it receives a page. And I stumbled: I think curl is advanced version of file_get_contents but they must not differ in fetching page. Where are the problems?
The error:Extra content at the end of the document
I don't know how but webpage returns some symbol at the end
With regards

Post XML via xmlhttpresponse and read xml from php

I have managed to send an xml via post using xmlhttprequest. I have also managed to read the whole xml syntax by an aspx page using
Dim reader As System.IO.StreamReader = New System.IO.StreamReader(Page.Request.InputStream)
Dim xmlData As String = ""
xmlData = reader.ReadToEnd()
I am now trying to read the xml from a php page. (I want to read the whole xml, headers and data)
using $_POST I am getting nothing
using file_get_contents("php://input") im getting the xml's data, no headers.
what am I doing wrong? How can I read the whole posted xml?
file_get_contents does the job i want.
althought mozilla displays the pure xml data file_get_contents has

Parse XML received from a GET request

As a part of a school project, i am making a call of the school's api.
Method: GET
Response Format: Xml
Now i use curl to make the web request.
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$data=curl_exec($ch);
curl_close($ch);
Now how do i parse the xml output to get the data i want?
Hope the question is clear
This depends on the exact nature of the content and, moreover, its structure.
The basics are to use DOMDocument (or, alternatively, simplexml, which I dislike as an API) to parse the document, then to use DOM traversal or XPath to find the content you want.
An example might look like this:
$dom = new DOMDocument;
$dom->loadXML($data); // data from cURL request
$xpath = new DOMXPath($dom);
$names = $xpath->query('//student/name'); // find all name elements that are direct children of student elements
foreach ($names as $name) {
echo $name->nodeValue;
}
The exact code you want depends on the structure of the XML and what content you want to get out of it.
Use SimpleXML:
$xml = simplexml_load_string($data);
Take a look at SimpleXML, DOMDocument, XMLParser and XPath. I usually prefer SimpleXML, but as many people, as many opinions...
You will find lots of examples in the documentation to this PHP classes.
assuming that $data contains xml you can use SimpleXML to parse it

Categories