Skip to content

Instantly share code, notes, and snippets.

@stulevine
Created April 24, 2013 18:57
Show Gist options
  • Save stulevine/5454604 to your computer and use it in GitHub Desktop.
Save stulevine/5454604 to your computer and use it in GitHub Desktop.
<?php
/**
* A soap _client_ for accessing other apis
*
*/
namespace soap;
class Soap extends \core\LibBase {
private $soap_client;
public function create($wsdl, $endpoint, $auth_params=array(), $ttl=10)
{
}
}
class SoapCient {
private $client;
private $endpoint;
private $wsdl;
private $ttl;
private $auth_params;
private $soap_auth;
private $initialized = false;
private $soap_user;
private $soap_password;
public function __contruct($wsdl, $endpoint, $auth_params=array(), $soap_auth=array(), $ttl=15)
{
$this->wsdl = $wsdl;
$this->endpoint = $endpoint;
$this->ttl = $ttl;
$this->auth_params = $auth_params;
$this->soap_auth = $soap_auth;
$this->client = $this->client();
$this->initialized = true;
}
public function call($method, $params=array(), $client=null)
{
if (!$this->initialized)
throw new SoapException(
"Please initialize the soap client before calling this method.", NULL, NULL, 403);
if (empty($client))
$client = $this->client;
if (!empty($this->auth_params)) {
$params = array_merge($params, $this->auth_params);
}
try {
if (empty($params)) {
$data = $client->$method();
}
else {
$data = $client->$method($params);
}
return json_decode(json_encode($data), true);
}
catch (Exception $e) {
throw new SoapException(
"Soap Error" . $e->getMessage(), NULL, NULL, $e->getCode());
}
}
private function client()
{
$params = array(
'location'=>$this->endpoint,
'connection_timeout'=>$this->ttl
);
// $soap_auth = array('login'=> <soap_user>,'password'=><soap_pw>')
if (!empty($this->soap_auth))
$params = array_merge($params, $this->soap_auth);
try {
$client = @new \SoapClient($this->wsdl, $params);
}
catch (\SoapFault $fault) {
throw new SoapException(
"Backend Error" . $fault->getMessage(), NULL, NULL, $fault->getCode());
}
return $client;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment