SOAP API Code Examples

August 22, 2011

The Call Class

<?php
 
class SoftlayerApiCall
{
    /**** Properties ****/
    protected $_service = "SoftLayer_Account";
    protected $_method = "getObject";
    protected $_init = null;
 
    protected $_wsdl = "https://api.softlayer.com/soap/v3/SoftLayer_Account?wsdl";
    protected $_typeNameSpace = "http://api.softlayer.com/soap/v3/SLTypes/";
 
    protected $_username;
    protected $_password;
 
    protected $_clientOptions = Array("trace"=>true);
    protected $_client;
 
    protected $_response;
 
 
    /**** Methods ****/
        /**** Constructors ****/
    public function __construct(array $Setup = null)
    {
        If ( is_array($Setup) ){
            $this->setOptions($Setup);
        }
    }
 
    public function setOptions(array $setup)
    {
        $methods=get_class_methods($this);
        foreach($setup as $key => $value){
            $method = 'set'.ucfirst($key);
            if (null != $value &amp;&amp; "" != $value){
                if( in_array($method,$methods) ){
                    $this->$method($value);
                }else{ throw new Exception($method.' not found in methods and '.$value.' not saved'); }
            }
        }
        return $this;
    }
 
    public function __set($name, $value)
    {
        $method = 'set'. ucfirst($name);
        if( !method_exists($this,$method) ){
            throw new Exception('Tried to set an unrecognised Property');
        }
        if (null != $value &amp;&amp; "" != $value){ return $this->$method($value); }
    }
 
    public function __get($name)
    {
        $method = 'get'. ucfirst($name);
        if( !method_exists($this,$method) ){
            throw new Exception('Tried to get an unrecognised Property');
        }
        return $this->$method();
    }
 
        /**** Actions ****/
    public function Response()
    {
        $Wsdl = $this->getWsdl();
        $Options = $this->getClientOptions();
        $this->setClient($Wsdl,$Options);
        $this->createHeaders();
        $Method = $this->getMethod();
        try {$Response = $this->getClient()->$Method();}
          Catch(Exception $e){$error=$e->getMessage(); $Response=array("Error:"=>$error);}
        return $Response;
    }
 
    public function createHeaders(){
        $Headers = Array();
        $Username=$this->getUsername();
        $ApiKey=$this->getPassword();
        $TypeNameSpace=$this->getTypeNameSpace();
        $Service=$this->getService();
        $InitId=$this->getInit();
 
        $AuthUser=new SoapVar($Username,XSD_STRING,null,null,'username');
        $AuthKey=new SoapVar($ApiKey,XSD_STRING,null,null,'apiKey');
        $Auth=array($AuthUser,$AuthKey);
        $AuthHeader=new SoapVar($Auth,SOAP_ENC_OBJECT,'slt:authenticate',$TypeNameSpace,'authenticate');
        $Headers[]=new SoapHeader("http://api.softlayer.com/soap/v3/SLTypes/","authenticate",$AuthHeader);
 
        If (null!=$InitId){
            $Init = new SoapVar($InitId,XSD_INT,null,null,"id");
            $Init = array($Init);
            $InitHeader=new SoapVar($Init,SOAP_ENC_OBJECT,'v3:'.$Service.'InitParameters',$TypeNameSpace,$Service.'InitParameters');
            $Headers[]=new SoapHeader("http://api.softlayer.com/soap/v3/SLTypes/",$Service."InitParameters",$InitHeader);
        }
        $this->getClient()->__setSoapHeaders($Headers);
    }
 
    /**** Methods For Setting and Getting Properties ****/
 
        /**** Sets ****/
    public function setService($Service){ $this->_service = $Service; }
    public function setMethod($Method){ $this->_method = $Method; }
    public function setInit($Init){ $this->_init = $Init; }
 
    public function setWsdl($Service){ $this->_wsdl="https://api.softlayer.com/soap/v3/".$Service."?wsdl"; }
    public function setTypeNameSpace($typeNameSpace){ $this->_typeNameSpace = $typeNameSpace; }
 
    public function setUsername($username){ $this->_username=$username; }
    public function setPassword($password){ $this->_password=$password; }
 
    public function setClientOptions($Options){ $this->_clientOptions=$Options; }
    public function setClient($Wsdl, $Options){
        if (null!=$Wsdl){
            $Client = new SoapClient($Wsdl,$Options);
            $this->_client = $Client;
       }else{ throw new Exception( 'Tried to set Client without specifying a Web Service Definition Language' ); }
    }
 
    public function setResponse($Response){ $this->_response=$Response; }
 
        /**** Gets ****/
    public function getService(){ return $this->_service; }
    public function getMethod(){ return $this->_method; }
    public function getInit(){ return $this->_init; }
 
    public function getWsdl(){ return $this->_wsdl; }
    public function getTypeNameSpace(){ return $this->_typeNameSpace; }
 
    public function getPassword(){ return $this->_password; }
    public function getUsername(){ return $this->_username; }
 
    public function getClientOptions(){ return $this->_clientOptions; }
    public function getClient(){ return $this->_client; }
 
    public function getResponse(){ return $this->_response; }
}

The View

<html>
<head></head>
<body>
<?php include("SoftLayerApiController.php"); ?>
<div id="form">
  <form method="post" action="View.php">
    <span>Service: </span><input type="text" name="service"></input><br/><br/>
    <span>Method: </span><input type="text" name="method"></input><br/><br/>
    <span>Init Properties: </span><input type="text" name="init"></input><br/><br/>
    <button type="submit">Submit</button>
  </form>
</div>
<div id="content">
<table>
<?php foreach($Result as $key=>$value): ?>
<tr>
  <td>
    <?php echo $key; ?>
  </td>
  <td>
    <?php if(is_object($value)): ?>
      <table>
      <?php foreach($value as $subkey=>$subvalue): ?>
        <tr>
          <td><?php echo $subkey; ?></td>
          <td><?php echo $subvalue; ?></td>
        </tr>
      <?php endforeach; ?>
      </table>
    <?php else: print_r($value); ?>
    <?php endif; ?>
  </td>
</tr>
<?php endforeach; ?> 
</table>
</div>
</body>
</html>

The Controller

<?php
 
include("SoftLayerApiCall.php");
include("Vars.php");
 
$Setup = Array();
 
$Setup['service'] = $_POST['service'];
$Setup['method'] = $_POST['method'];
$Setup['init'] = $_POST['init'];
$Setup['wsdl'] = $_POST['service'];
 
$Setup['username'] = $username;
$Setup['password'] = $apiKey;
 
$Call = new SoftLayerApiCall($Setup);
$Result = $Call->Response();

Leave a Reply

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <pre>, <blockcode>, <bash>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Keywords:
Categories:

Leave a Reply

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <pre>, <blockcode>, <bash>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.