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

mbn
  • 151
  • 5

3 Answers3

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'; ) );
Dharman
  • 30,962
  • 25
  • 85
  • 135
Kapil Yadav
  • 650
  • 1
  • 5
  • 29
  • Interesting, Could you be more specific with SOAP API URL to check if there are any additional requirements while calling the API? – Kapil Yadav Sep 19 '19 at 12:49
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

-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.

yariv_kohn
  • 94
  • 4