'¿How can i consume a SOAP webservice with PHP?

I think there are some methods:

Screenshot of Wizdler

Well i try to login using nusoap, curl and SoapClient but no luck.

My WSDL knowledge is as poor as my english so please i need some help to deal with this.

$baseurl='https://dcs.honda.com.ar/WCF/Service.svc?wsdl';

My best try with cURL:

        $curl = curl_init();

        $url = $this->baseurl;
        $payload = '<Envelope xmlns="http://www.w3.org/2003/05/soap-envelope">
        <Header>
            <client_id xmlns="http://tempuri.org/">' . $clientId . '</client_id>
            <cliente_secret xmlns="http://tempuri.org/">' . $clientSecret . ']</cliente_secret>
        </Header>
        <Body>
            <AuthRequest xmlns="http://tempuri.org/"/>
        </Body>
    </Envelope>';
        $headers = [
            "Content-type: application/soap+xml;charset=\"utf-8\"",
            'Accept: application/soap+xml',
            'Cache-Control: no-cache',
            'Pragma: no-cache',
            'Content-length: ' . strlen($payload),
        ];

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

        $response = curl_exec($curl);
        var_dump($response);
        die();

Response => string(576) "http://www.w3.org/2005/08/addressing/soap/faults:Sendera:InvalidSecurityAn error occurred when verifying security for the message."

SoapClient try:

<?php
ini_set('default_socket_timeout', 600);

function WebServices($function, $parameters)
{
    $username = 'ausername';
    $password = 'apassword';

    $url = 'https://dcs.honda.com.ar/WCF_Test/Service.svc?wsdl';
    $service_url = 'https://dcs.honda.com.ar/WCF_Test/Service.svc';

    $client = new SoapClient($url, [
        'soap_version' => SOAP_1_2,
        'Username' => $username,
        'Password' => $password,
        'SOAPAction' => "http://tempuri.org/IService/$function",
        'cache_wsdl' => WSDL_CACHE_NONE, // WSDL_CACHE_MEMORY
        'trace' => 1,
        'exception' => 1,
        'keep_alive' => false,
        'connection_timeout' => 500000,
    ]);
    $action = new \SoapHeader(
        'http://www.w3.org/2005/08/addressing',
        'Action',
        "http://tempuri.org/IService/$function"
    );
    $to = new \SoapHeader(
        'http://www.w3.org/2005/08/addressing',
        'To',
        $service_url
    );
    $security = generateWSSecurityHeader('PasswordText', $password);

    //var_dump($security);
    //die();

    $client->__setSoapHeaders([$action, $to, $security]);
    try {
        return $client->__soapCall($function, $parameters);
    } catch (SoapFault $e) {
        return $e->getMessage();
    }
}

function generateWSSecurityHeader($passwordType = 'PasswordText', $password)
{
    $username = 'ausername';
    $password = 'apassword';
    $nonce = mt_rand();
    $OASIS = 'http://docs.oasis-open.org/wss/2004/01';
    $timestamp = gmdate('Y-m-d\TH:i:s\Z');

    if ($passwordType === 'PasswordText') {
        $password = $password;
        $nonce = sha1(mt_rand());
    } else {
        return '';
    }

    $xml =
        '
<wsse:Security SOAP-ENV:mustUnderstand="1" xmlns:wsse="' .
        $OASIS .
        '/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <wsse:UsernameToken>
    <wsse:Username>' .
        $username .
        '</wsse:Username>
    <wsse:Password Type="' .
        $OASIS .
        '/oasis-200401-wss-username-token-profile-1.0#' .
        $passwordType .
        '">' .
        $password .
        '</wsse:Password>
    <wsse:Nonce EncodingType="' .
        $OASIS .
        '/oasis-200401-wss-soap-message-security-1.0#Base64Binary">' .
        $nonce .
        '</wsse:Nonce>';

    if ($passwordType === 'PasswordDigest') {
        $xml .=
            "\n\t" .
            '<wsu:Created xmlns:wsu="' .
            $OASIS .
            '/oasis-200401-wss-wssecurity-utility-1.0.xsd">' .
            $timestamp .
            '</wsu:Created>';
    }

    $xml .= '
    </wsse:UsernameToken>
</wsse:Security>';

    return new SoapHeader(
        $OASIS . '/oasis-200401-wss-wssecurity-secext-1.0.xsd',
        'Security',
        new SoapVar($xml, XSD_ANYXML),
        true
    );
}

$params = [
    'client_id' => 'ausername',
    'cliente_secret' => 'apassword',
];

var_dump(WebServices('getAccess', $params));

Response => object(SoapHeader)#4 (4) { ["namespace"]=> string(81) "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" ["name"]=> string(8) "Security" ["data"]=> object(SoapVar)#5 (2) { ["enc_type"]=> int(147) ["enc_value"]=> string(619) " alongrandomalphanumericcharactersstring... " } ["mustUnderstand"]=> bool(true) }

Im lost and don't know how to proceed. Apreciate so much any kind of help.

Thanks in advance!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source