Good day!
This post is intended for beginners who are starting to deal with OOP on php, and who are trying to act in accordance with this style.
It will be about expanding the class SoapClient (). What it is and what it is eaten with, along with the installation is described in
this post .
')
Specifically, I asked myself the question of working with soap, when at work I received a task about the interaction of our applications with the customer's servers. Since Most of the logic in our applications is written in a procedural style, and I was originally going to stuff the work with soap into several functions. But when I realized that I’ve gotten at least - ugly, very cumbersome, and rather inconvenient, I decided to expand the class SoapClient.
So let's get started.
The task was set before me - to develop the logic of the system of interaction of the application with the customer’s server for receiving various data. I will not give all the methods, I’ll consider using one example: get the full name of the manager by company name and region code.
There was little information in Google, but obeying pure logic, I had such a class:
class SoapCrmClient extends SoapClient { function getPersonaByCompany($companyName, $regionCode) { $dataSoap = $this->FindByCompany(array("CompanyName"=>$companyName, "RegionCode"=>$regionCode)); return $dataSoap->FindByCompanyResult; } }
Working with soap was done in wsdl mode, and creating instances of the class, always setting the path to one wsdl file, was not desirable, so the __construct () function was added:
function __construct() { $this->SoapClient("data/Service1.wsdl"); }
Also, the applications with which I work, due to some factors - in the cp1251 encoding, and SOAP works with utf-8. Initially, I wrote a cumbersome construct using mb_convert_encoding, but, referring to the documentation of the SoapClient class, I saw the following:
In the number of parameters that SoapClient takes - there is an encoding parameter - the encoding from which (at the input), and into which (at the output) it converts the data of the Soap. We use this:
function __construct() { $this->SoapClient("data/Service1.wsdl", array('encoding'=>'CP1251')); }
Now we combine 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) {