SoapFault: No id attribute on element http://schemas.xmlsoap.org/soap/envelope/:Body (code wsse:InvalidSecurity)
Asked Answered
M

3

13

I'm new to SOAP and trying to connect to a SOAP server using PHP's built-in SoapClient. I am receiving this error for days now and didn't find any solution.

This is for a Linux Server. I've tried to include SoapHeaders with timestamps and add different parameters to the request, but still have not succeeded.

This is my code:

    $params = array(
        'X' => array(
            'Y' => '0',
            'Z' => '1',
            'Q' => array(
                'W' => array(
                    'E' => '1'
                )
            ),
            'R' => array(
                'T' => 3,
                'Y' => array(
                    'U' => 'x',
                    'I' => 'y'
                )
            ),
            'O' => array(
                'P' => '2'
            )
        ),
        'A' => array(
            'S' => array(
                'D' => 'H',
                'F' => 'J',
                'G' => 'K'
            )
        )
    );

    $client = new SoapClient( 'https://path-to-wsdl', array( 'trace' => 1 ) );

    $timestamp = gmdate("Y-m-d\TH:i:s\Z");
    $timestamp_expires = gmdate("Y-m-d\TH:i:s\Z", strtotime( '+1 hour' ) );


    $authHeader = new stdClass();
    $authHeader->Timestamp->Created = $timestamp;
    $authHeader->Timestamp->Expires = $timestamp_expires;
    $Headers[] = new SoapHeader('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'Security', $authHeader,TRUE);

    // $client->__setSoapHeaders($Headers);

    try
    {
        $response = $client->SoapFunction( $params );
    }
    catch( SoapFault $fault )
    {
        echo 'Request : <br/><xmp>',
        $client->__getLastRequest(),
        '</xmp><br/><br/> Error Message : <br/>',
        $fault->getMessage();

        die;
    }

I expect a response from the server, but receive this error code instead:

No id attribute on element http://schemas.xmlsoap.org/soap/envelope/:Body

Edit:

Code wsse:InvalidSecurity

Moderato answered 4/9, 2019 at 8:3 Comment(3)
Please share WSDL file too.Cachinnate
May be this question will be helpful #420063Mistassini
@JitendraYadav The WSDL file is private, but is used by other companies too (and working). But there is not a lot of documentation on it unfortunatelyModerato
I
0

I believe there should be a certificate security (.pem file) associated with the SOAP API you are calling. Once you pass that certificate path along with SOAP request, it should work.

For adding certificate in your SOAP request, you just need modify your SOAP call like below code.

$client = new SoapClient( 'https://path-to-wsdl', array( 'trace' => 1, local_cert' => dirname(__FILE__) . '/certificatefile.pem'; ) );
Infancy answered 19/9, 2019 at 12:1 Comment(1)
Interesting, Could you be more specific with SOAP API URL to check if there are any additional requirements while calling the API?Infancy
O
0

I've tried using SoapClient from PHP and maybe it's just me but my experience has been absolutely horrible. You should give CURL a try. Also, here's the approach I use for WSDL requests:

  1. Make it work on a client like Postman
  2. Once you've got the correct format and headers, you can copy the generated PHP code from postman and focus on making it dynamic within your own environment
  3. Print out your XML requests and compare it to working Postman requests

I know it's not the best approach, but if I had a dollar for every time I started debugging SoapClient requests and switched to CURL, I'd be a millionaire already

Opaque answered 2/8, 2022 at 6:30 Comment(0)
S
-1

It is possible that the authHeader should be a other than stdClass. Try to use this tool:

https://github.com/wsdl2phpgenerator/wsdl2phpgenerator

It will generate PHP classes out of your wsdl file. Using these classes it will be easier to follow the structure of how to build a request and what exactly you need to send.

Semblable answered 18/9, 2019 at 6:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.