PHP soap web service client.

create a general class to call the web service via SOAP.

class wsExtranet 
{
const NS = "carrefourWebserviceArgos";

public $is_valid = true;

private $ws_client;
private $logFilePath = '/media/wsLogs/';
private $debug = false;

private $wsdl = "http://srvweb1.o2m-services.net/WS_BUYBACK_WEB/awws/WS_Buyback.awws?wsdl";

public function __construct($debug = false)
{
// initialize soap client to call we service
try {
$this->ws_client= new SoapClient($this->wsdl,
array(
'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
'cache_wsdl' => strpos($_SERVER['HTTP_HOST'], '.loc.') === false ? WSDL_CACHE_BOTH : WSDL_CACHE_NONE
)
);

if((isset($_REQUEST['debug']) && $_REQUEST['debug'] == 'true') || $debug == true)
$this->debug = true;

}
catch (SoapFault $fault) {
$this->is_valid = false;

}
}

public function log($service, $params, $reponse, $url = null)
{
$dir = PELIAS_SITE_ROOT . $this->logFilePath . date("Y-m-d") . "/";
if(!is_dir($dir))
{
mkdir($dir,0755,true);
}
$url = isset($url) && $url ? $url : $this->wsdl;

$heure = date("H");
$file_handle = fopen($dir . $heure . "_ws.log", 'a');
$date_time = date("Y-m-d H:i:s");
$message = $date_time . " ########################## \r\n";
$message .= "Service: $service ; Url: $url; \r\n";
$message .= "Params: " . print_r($params, true) . "; \r\n";
$message .= "Response: " . print_r($reponse, true) . "; \r\n";

fwrite($file_handle, $message);
fclose($file_handle);
}

public function callService($service, $infosWS)
{
if(!$this->is_valid)
{
return false;
}

try {
$reponse = $this->ws_client->$service($infosWS);
//log into file
if($this->debug)
$this->log($service, $infosWS, $reponse);


}
catch (SoapFault $fault) {
//log error
if($this->debug)
$this->log($service, $infosWS, $reponse);
return false;
}

return $this->soapvar_decode($reponse);
}

private function soapvar_decode($var)
{
if($var instanceof SoapVar)
{
return $this->soapvar_decode($var->enc_value);
}
else if(is_array($var) || is_object($var))
{
$tmp_array = array();

foreach($var as $key => $value)
{
$tmp_array[$key] = $this->soapvar_decode($value);
}

return $tmp_array;
}
return $var;
}

}

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s