PHP SOAP Sample

Modified on Fri, 01 Feb 2013 09:59 by CM — Categorized as: PHP, SOAP API, Tutorials

The following sample shows how to use the SOAP API from PHP5.


To create the client, we suggest using a tool for code-generation such as wsdl2php you can find here:

Note: The generator requires PEAR. PHP5 users should install PEAR for PHP5 by running “pear5 install”.

When running the code generator against the WSDL for the PSWinCom Gateway SOAP Interface, you will get code as shown below (not all methods are shown, just the minimum required to submit a test message):

File SMSMessage.php

<?php
/**
 *
 *
 * @package
 * @copyright
 */
class SMSMessage {
  /* string */
  public $ReceiverNumber;
  /* string */
  public $SenderNumber;
  /* string */
  public $Text;
  /* string */
  public $Network;
  /* string */
  public $TypeOfMessage;
  /* int */
  public $Tariff;
  /* int */
  public $TimeToLive;
  /* string */
  public $CPATag;
  /* boolean */
  public $RequestReceipt;
  /* string */
  public $SessionData;
  /* string */
  public $AffiliateProgram;
  /* string */
  public $DeliveryTime;
}
?>

File SendSingleMessage.php

<?php
/**
 *
 *
 * @package
 * @copyright
 */
class SendSingleMessage {
  /* string */
  public $username;
  /* string */
  public $password;
  /* SMSMessage */
  public $m;
}
?>

File SMSService.php

<?php
/**
 * SMSService class file
 *
 * @author {author}
 * @copyright {copyright}
 * @package {package}
 */
  
/**
 * SendSingleMessage class
 */
require_once 'SendSingleMessage.php';

/**
 * SMSMessage class
 */
require_once 'SMSMessage.php';

/**
 * SMSService class
 *
 * PSWinCom SMS Gateway SOAP Interface
 *
 * @author {author}
 * @copyright {copyright}
 * @package {package}
 */
class SMSService extends SoapClient {

  public function SMSService($wsdl = "http://sms.pswin.com/SOAP/SMS.asmx?wsdl", $options = array()) {
    parent::__construct($wsdl, $options);
  }

  /**
   *
   *
   * @param SendSingleMessage $parameters
   * @return SendSingleMessageResponse
   */
  public function SendSingleMessage(SendSingleMessage $parameters) {
    return $this->__call('SendSingleMessage', array(
           new SoapParam($parameters, 'parameters')
    ),
    array(
          'uri' => 'http://pswin.com/SOAP/Submit/SMS',
          'soapaction' => ''
         )
    );
  }
}
?>

File Test.php

<?php

require_once('SMSService.php');

// Create a new message
$objMessage = new SMSMessage();
$objMessage->ReceiverNumber ='(receiver number including countrycode)';
$objMessage->SenderNumber = 'PSWinCom';
$objMessage->Text = 'Test message';
$objMessage->Tariff = 0;
$objMessage->TimeToLive = 0;
$objMessage->RequestReceipt = false;

// Create parameters
$objSendSingleMessage = new SendSingleMessage();
$objSendSingleMessage->m = $objMessage;
$objSendSingleMessage->username = '(username)';
$objSendSingleMessage->password = '(password)';

// Connect to service
$objService = new SMSService();

// Send message
$objReturn = $objService->SendSingleMessage($objSendSingleMessage);

echo '<pre>';
var_dump($objReturn);
echo '</pre>';

?>