A
A
Andrey Surzhikov2021-04-01 15:05:10
PHP
Andrey Surzhikov, 2021-04-01 15:05:10

How to work with WSDL / SOAP in PHP?

I am writing integration with the service of the Federal Tax Service.
Unfortunately, they use SOAP for this, which I last heard about in computer science classes at the university;

They sent a link to the WSDL: https://himself-ktr-api.nalog.ru:8090/open-api/Aut... which describes the authorization service.

1) What are the most correct methods of working with this type of API from PHP?
2) Are there any proven packages to parse WSDL to form SOAP requests?
3) Where can I get the XSD schema for the request itself (I thought it should be in the WSDL, but it's not there.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2021-04-01
@Rsa97

https://www.php.net/manual/en/book.soap.php

S
Sergey delphinpro, 2021-04-01
@delphinpro

I used this package https://github.com/notfalsedev/laravel-soap

example

class SoapController extends Controller
{
    /** @var \Artisaninweb\SoapWrapper\SoapWrapper */
    protected $soap;

    /** @var \Artisaninweb\SoapWrapper\Client */
    protected $client;

    public function __construct(SoapWrapper $soap)
    {
        $this->soap = $soap;
        $this->client = null;
    }

    public function getComputers()
    {
        try {
            $clientId = request()->get('clientId', 0);
            $orgId = request()->get('orgId', 0);
            $this->initSoap();
            $response = $this->client->call('GetComps', [
                [
                    'ClientId' => $clientId,
                    'OrgId'    => $orgId,
                ],
            ]);

            $computers = json_decode($response->GetCompsResult, true);
            return response()->json($computers, 200);

        } catch (\Throwable $e) {
            return response()->json([
                'message' => $e->getMessage(),
            ], 500);
        }
    }

    /**
     * @throws \Exception
     */
    protected function initSoap()
    {
        $clientName = 'some_name';
        $wsdl = config('app.soap_wsdl');
        if (!$wsdl) throw new \Exception('Invalid wsdl');
        $version = $this->getSoapVersion();
        $this->soap->add($clientName, function (SoapService $service) use ($wsdl, $version) {
            $service
                ->wsdl($wsdl)
                ->options(['soap_version' => $version]);
        });

        $this->client = $this->soap->client($clientName, function ($client) { return $client; });
    }

    /**
     * @return int
     * @throws \Exception
     */
    protected function getSoapVersion()
    {
        switch (config('app.soap_version')) {
            case '1.1':
                return SOAP_1_1;
            case '1.2':
                return SOAP_1_2;
            default:
                throw new \Exception('Invalid SOAP version');
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question