Article written

  • on 04.05.2009
  • at 04:46 AM
  • by Robert Banh

PHP and SOAP 0

Apr5

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);
}

subscribe to comments RSS

There are no comments for this post

Please, feel free to post your own comment

* these are required fields

All works are licensed under the
Creative Commons Attribution-Share Alike 3.0 Unported License.