Here’s something I don’t do very often, but I’ve written a quick procedural script that could easily be turned into a class that will submit data to a HTTP POST “webservice”. It wouldn’t be a web service per se, rather than just a PHP script that accepts POST parameters, as opposed to an actual SOAP endpoint.
<?php
// Put the URL of your "POST endpoint"
$url = 'http://some/script/that/accepts/POST/input.php';
// Add your field pairs to this array...
$field_pairs = array(
array("field1", "avalue"),
array("field2", "bvalue"),
);
// This bit prepares the fields by imploding them into a format similar to a query string...
$fields = array();
foreach($field_pairs as $field_pair)
{
$fields[] = implode("=", $field_pair);
}
$postfields = implode("&", $fields);
// This sends the request through cURL
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $postfields);
curl_exec($c);
curl_close($c);
?>
Has your host got allow_url_fopen = Off and they’re too stubborn to turn it on? Got this error?
Warning: simplexml_load_file() [function.simplexml-load-file]: URL file-access is disabled in the server configuration in /url/to/file.php on line 62
It’s a pain for writing cool API clients that add cool functionality into your website, like using the Last.fm API, Amazon Associates Web Services, or the YouTube API, because they all use XML based services that you have to retrieve.
The simplest way is to use file_get_contents($url);, but that doesn’t work if allow_url_fopen = Off.
The next simplest way is to use libcurl, which although is very useful, has given me a lot of grief throughout my programming life, so is sort of my arch nemesis. I tried it on 1and1 trying to access AAWS, but it didn’t work anyway.
The last solution is my hacky little method to draw wget results into the script. This method is probably the slowest as we’re calling an external binary on a shared web server, but it works.
$url = "http://api.somesite.com/rest/xml?query=string&goes=here";
$user_agent = "My Awesome API Client v1.0";
ob_start();
passthru("wget -U '{$user_agent}' -q -O - '{$url}'");
$response = ob_get_contents();
ob_end_clean();
$xml_document = simplexml_load_string($response);
SOAP in PHP is quite simple, and as it’s something I’m doing at work at the moment, I thought I’d copy a bit of code to remind myself how to do it. Also to remind myself about the idea Chris had for Private Passwords.
Creating the client:
// WSDL client
$client = new SoapClient($wsdl);
// Non-WSDL client
$client = new SoapClient(NULL, array(
"location" => $endpoint_url . $script_name,
"uri" => $endpoint_url);
It’s that easy to create the client, and just as easy to make the request itself:
// WSDL client
$result = $client->doSomething("argument");
// Non-WSDL client
$result = $client->__soapCall("doSomething", array("argument"));
As this is PHP, and PHP is lovely, the result is an object, and can be accessed as a normal object.
Chris here at Netbasic suggested making a Firefox plug-in for Private Passwords that will automatically connect to the PP server and fill in form passwords for you.
I can see this being fairly simple, perhaps using some kind of encrypted SOAP method. Mainly putting here to remind myself to look into it…
I’ve always disliked XML really, I just feel it’s a bit bloated as a data container and parsing it in some languages can be unnecessarily complex. For AJAX-style requests I much prefer to use a format like JSON to transport data. Unfortunately, I need to keep up with the times and XML is already a major part of the internet, and is used in SOAP, RSS, Atom etc.. Therefore I like this class that a developer Anis uddin Ahmad has made. There’s probably loads of other classes like it, but this one is very simple to use, and by reading the author’s blog post, this is the intention. So I refer you now to the PHP Universal Feed Parser. I’m not sure how versatile it is, and what could break it yet, but it’s a cool idea and once I get a chance I’ll have a look at the source code and see how it works.