I'm working on a system which needs to integrate with an external (commercial) system. On my site, I have some information, and then that needs to be checked against the external system's records. This is done by posting an XML request.
I can't make sense of their documentation. They've given me some example xml, but I just don't know what to do with it. Can anyone help?! I can't find much online that tells me what to do.
I'm using PHP, and the request has to be POSTed via HTTPS. The xml I've been given is (roughly - can't give the full details due to security):
<?xml version="1.0" standalone="no" ?>
<request>
<request_header interface_version_no="0.2" dtd_version_no="0.2">
<client_reference>Test Client</client_reference>
</request_header>
<service_request id="1">
<session_service>
<logon_request domain_id="******" user_id="******" password="******"/>
</session_service>
</service_request>
</request>
The code above should log me in to their system. So, my questions are:
What do I actually do with this? How do I make it into a POST request?
What do I do with the response (which is also xml)? I have parsed xml before (using XSLT), but that was to display it... is there a standard way of doing it in PHP?
Thanks! Any help is appreciated. I can't find much online (well, I can find loads, but none of it seems to be specific to what I'm looking for, so I'm getting confused), so even links to the appropriate help pages would be great.
Sending HTTP request, in PHP, is generally done using the curl extension, which provides a lot of features.
See for example the page of curl_setopt(), which lists all the options that can be used with curl -- there are some in there that should help you, for instance with the POSTing stuff.
Another solution, if allow_url_fopen is enabled on your server, would be to use the stream functionnality of PHP.
You could the use the file_get_contents() function, providing custom context options -- see this page : HTTP context options (the example #1 is interesting, in your case ;-) )
If you get XML as a response, you'll have to parse that result.
This is generally done, in PHP, using either DOMDocument, if you need to modify or manipulate your XML a lot... or with SimpleXML if you just need to read the XML and extract some data from it.
Here, you'll most probably need to use the simplexml_load_string function ;-)
Related
i have problem in my website as i can't generate sitemap form site https://www.xml-sitemaps.com/ , every time i generate it get empty sitemap with this comment
" This XML file does not appear to have any style information associated with it. The document tree is shown below."
and i really don't know how can i solve it
so please could anyone help me in this problem
That's not an error. That's absolutely as expected. Sitemaps aren't for you to look at, they are designed for a machine to process.
Reference
XML and HTML are like cousins. They share a lot of it's syntaxis, but html has some standardized semantics, while the semantics of xml is totally up to you, the creator of a xml document.
So what happens here; you're browser is reading xml, but a tag is not considered something to be outputted, it is considered to be describing something, which can be outputted on a certain way, if there is styling information available.
So what you see is the browser (probably IE; get a proper browser) telling you it doesn't know how to present the xml file in a proper way and deciding it will not show it at all. But if you look to the source (ctrl+u or cmd+u), your sitemap will be there.
Although in contrast to what #ShakirKhan says; xml is designed to be looked at both by humans as well as computers. So it is understandable, but it should not happen. You should just see the xml file (so, get a proper browser)
I need to extract the "Toner Cartridges" levels from This site and "send it" to the one im working on. Im guessing i can use GET or something similar but im new to this so i dont know how it could be done.
Then the information needs to be run through a if/else sequence which checks for 4 possible states. 100% -> 50%, 50%->25%, 25%->5%, 5%->0%.
I have the if/else written down but i cant seem to find any good command for extracting the infromation from the index.php file.
EDIT: just need someone to poin me in the right the direction
To read the page you can use file_get_contents
$page = file_get_contents("http://example.com");
But in order to make the function work with URLs, allow_url_fopen must be set to true in the config file of your php (php.ini)
Then you can use a regular expression to filter the text and get data.
The php function to perform a regular expression is preg_match
Example:
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}\n";
will output
domain name is: php.net
I imagine you are reading from a Printer Status Page. In which case, to give your self the flexibility to use sessions and login, I would look into Curl. Nice thing about Curl is, you can use the PHP library for code, but you can also test at the command-line rather quickly.
After you are retrieving the HTML contents, I would look into using an XML parser, like SimpleXML or DOMDocument. Either one will get you to the information you need. SimpleXML is a little easier to use for people new to traversing XML (this is, at the same time, like and very not like jQuery).
Although, that said, you could also hack to the data just as quick (if you are just now jumping in) with Regulair Expressions (it is seriously like that once you get the hang of it).
Best of luck!
Now before you get out the torches and rail against spammers, I'll explain my intent here. I have written a series of scripts which scrape a certain website for contact information. These contacts are highly focused and are likely in a position where they are in need of a specific service I offer. The messages I plan on sending to them are one-offs and are written to be very helpful and respectful.
Now having said that, I'm having a hard time finding information on how to write a PHP bot that can enter a website, access a form, and send it. Everything I find is about stopping "spambots", unsurprisingly. I'm not worried about duping recaptchas or anything like that. If they have measures like that in place, I'm fine skipping them.
This question is too broad, so I have to give you a broad answer too...
First you need to download the page. You can use cURL (or file_get_contents might sufice).
Then you need to parse it with an HTML parser. You can use DOMDocument that comes bundled with PHP but you'll probably choke since DOMDocument is not very forgiving about pages with HTML syntax errors (or HTML5, for that matter)
Then you need to traverse the DOM and look for the form itself, extract the url and the method and make a request.
You can then use cURL to send a submit request to that url.
However, this will fail for dynamic pages (for instance, angular and other heavy javascripted pages). You probably better to use a headless browser like phantomjs.
I am trying to compile a Soap call with PHP (the params part of the call), but I am having trouble converting the XML nodes to PHP Soap to make the call.
I am trying to take this XML
https://gist.github.com/Davidlab/f40f386c65347f6d21e4
and convert it to a PHP soap (the params part of it).
If you look at the XML file you will see that there are 2 nodes and I need to translate that to this PHP sample that has only one node.
https://gist.github.com/Davidlab/e3cc11a7c80a4c273d31
I can get it to work with one but can not figure out the proper way to include another within the call.
Thanks for any help you may provide.
Use the built in SOAP functions in PHP. I just went through figuring out SOAP. After lots of Googling and emailing back and forth with tech support I figured out nobody really knows what SOAP is. :) Anyway the built in SOAP functions do all of the conversions for you. Very simple to use.
The easiest way to figure out how your data structures should look is to do a little bit of discovery like this:
$wsdl = "http://the-location-of-the-wsdl";
$soapClient = new SoapClient($wsdl);
var_dump($soapClient->__getFunctions());
var_dump($soapClient->__getTypes());
__getFunctions() will show you all the methods you can call, the data types they accept and return. __getTypes() will show you what the data structures look like. Create your arrays or classes so they follow the same structure. This is much easier than trying to figure it out with raw XML.
I understand that within same folder, I can use include() function for external PHP file, but now I would like to call the function in another PHP file which located in another URL.
For example, my live website (liveexample.com/table.php) has drop-down list and table, but without data.
My another PHP file (dataexample.com/data.php) is connected to database and process to extracting data out. But, it is in another server.
I need to make my data on [dataexample.com/data.php] delivers to [liveexample.com/table.php] and let the looping to draw table with data out on [liveexample.com/table.php] page.
Anyone has idea to design this method of delivering data from another server to another by using function call in PHP?
Or any other better solution to deliver my data between two different servers such as make the data record set into array and send to [liveexample.com/table.php]?
Please give me advise or consultation. Appreciate much!
I think SOAP webservice would be perfect for you to attain what you want but if possible just copy the same codes you have from the separate server.
If you make [dataexample.com/data.php] output your data as XML, then you can use it as a web service. What that means is, you can take that XML output (by sending a request the the data URL), and then parse it to load the data. This way, you can use that service any way you want. One way would be like you wanted, other examples would be via AJAX, or Flash etc.
So here are a few topics worth looking into:
using PHP for web services: http://wso2.org/library/3032
parsing XML data: http://www.w3schools.com/php/php_xml_simplexml.asp
I hope this will give a pretty good idea of how to achieve what you want to accomplish, because there a few options you can go by. Like Cristopher said, SOAP is one of them.
Have a great day.