PHP and SOAP 0
I’m not a fan of PHP/SOAP but depending on your project, you may need to use a SOAP protocol to access certain API or Webservices.
Being a lazy person, I will post my set of notes and how I build PHP SOAP calls as a reference guide to anyone who may find it useful.
You’ll notice two important functions that aren’t discuss widely:
__getFunctions
__getTypes
These are lifesavers because if you don’t like to read documentations (like myself), then you can run those 2 commands and reverse engineer the available APIs.
<?
// ========================
// Insert the path to the wsdl file.
// This is an example of Confluence's wsdl file.
// ========================
$client = new SoapClient(
"http://localhost:8080/rpc/soap-axis/confluenceservice-v1?wsdl"
,array("trace" => 1, "exceptions" => 0)
);
// ========================
// Displays all functions
// ========================
echo "<h2>All Functions</h2>";
$functions = $client->__getFunctions();
print_r($functions);
// ========================
// Display all Types
// ========================
echo "<h2>All Objects and Types</h2>";
$types = $client->__getTypes();
print_r($types);
// ========================
// Login API call for Confluence.
// ========================
$token = $client->login("admin", "secretPasswd");
echo "<p>Token: $tok</p>";
// ========================
// Pull all Confluence Spaces.
// ========================
$spaces = $client->getSpaces($token);
echo "<h2>All Spaces</h2>";
print_r($spaces);
?>
The above example is great for initial testing. But on production, you’ll want to wrap the SOAP calls in try/catch statements or just call is_soap_fault method to exit gracefully.
$token = $client->login("admin", "secretPasswd");
if (is_soap_fault($token)) {
trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
}
My name is Robert Banh. I am your typical web guy.
subscribe to comments RSS
There are no comments for this post