Extend SOAP class to PHP
Good day!
This post is intended for beginners starting to understand OOP in php and trying to act in accordance with this style.
It's about extending the SoapClient () class. What it is and what it is eaten with, coupled with the installation is described in this post .
Specifically, I asked myself the question of working with soap, when at work I got the task of how our applications interact with customer’s servers. Because Since most of the logic in our applications is written in a procedural style, then I was originally going to push the work with soap into several functions. But when I realized that it turns out at least - ugly, very bulky, and rather inconvenient, I decided to expand the SoapClient class.
So let's get started.
The task was set for me - to develop the logic of the system of interaction between the application and the customer’s server to receive various data. I will not give all the methods, I will consider an example of one: get the name of the manager by the company name and region code.
There was not much information in Google, but obeying pure logic, I got such a class:
We worked with soap in wsdl mode, and I didn’t want to create instances of the class, always prescribing the path to one wsdl file, so the __construct () function was added:
Also, the applications I'm working with are cp1251 encoded due to some factors, and SOAP works with utf-8. Initially, I wrote a cumbersome construct using mb_convert_encoding, but referring to the SoapClient class documentation, I saw the following:
Among the parameters that SoapClient accepts, there is an encoding parameter - the encoding from which (at the input) and into which (at the output) it converts data Soap. We use it like this:
Now we unite everything:
We work like this:
This post is intended for beginners starting to understand OOP in php and trying to act in accordance with this style.
It's about extending the SoapClient () class. What it is and what it is eaten with, coupled with the installation is described in this post .
Specifically, I asked myself the question of working with soap, when at work I got the task of how our applications interact with customer’s servers. Because Since most of the logic in our applications is written in a procedural style, then I was originally going to push the work with soap into several functions. But when I realized that it turns out at least - ugly, very bulky, and rather inconvenient, I decided to expand the SoapClient class.
So let's get started.
The task was set for me - to develop the logic of the system of interaction between the application and the customer’s server to receive various data. I will not give all the methods, I will consider an example of one: get the name of the manager by the company name and region code.
There was not much information in Google, but obeying pure logic, I got such a class:
class SoapCrmClient extends SoapClient {
function getPersonaByCompany($companyName, $regionCode) {
$dataSoap = $this->FindByCompany(array("CompanyName"=>$companyName, "RegionCode"=>$regionCode));
return $dataSoap->FindByCompanyResult;
}
}
We worked with soap in wsdl mode, and I didn’t want to create instances of the class, always prescribing the path to one wsdl file, so the __construct () function was added:
function __construct() {
$this->SoapClient("data/Service1.wsdl");
}
Also, the applications I'm working with are cp1251 encoded due to some factors, and SOAP works with utf-8. Initially, I wrote a cumbersome construct using mb_convert_encoding, but referring to the SoapClient class documentation, I saw the following:
Among the parameters that SoapClient accepts, there is an encoding parameter - the encoding from which (at the input) and into which (at the output) it converts data Soap. We use it like this:
function __construct() {
$this->SoapClient("data/Service1.wsdl", array('encoding'=>'CP1251'));
}
Now we unite everything:
class SoapCrmClient extends SoapClient {
function __construct() {
$this->SoapClient("data/Service1.wsdl", array('encoding'=>'CP1251'));
}
function getPersonaByCompany($companyName, $regionCode) {
$dataSoap = $this->FindByCompany(array("CompanyName"=>$companyName, "RegionCode"=>$regionCode));
return $dataSoap->FindByCompanyResult;
}
}
We work like this:
try {
$client = new SoapCrmClient();
$persona = $client->getPersonaByCompany('Майкрософт', 1);
} catch (SoapFault $e) { // ошибка SOAP канала
echo 'Ошибка SOAP-канала! '. $e->getMessage();
}