Connecting to a SOAP Web Service

I am using wp_remote_get ( &url , &args ) to connect to a legacy application (we have control of) via an exposed web service. Although I am able to connect to it, I am having problems understanding the mechanism to call methods within it and pass parameters to it. I understand the array construct but not sure what to poke where. I'm close , but no cigar. Note the service is SOAP (1.2) and not JSON.

If you could point me in some explicit examples I could follow, this would be of great assistance.

  • Ash
    • WordPress Hacker

    Hello @Peter

    Welcome to WPMU community!

    I hope you are well today and thanks for asking the question.

    I am tagging an available developer from second level support line in this thread for his valuable opinion on this issue. Please note that, developer response might be slower than usual staff response, so we appreciate your patience on this.

    Cheers
    Ash

  • Anang
    • New Recruit

    Hi Peter

    Is create a new plugin/functions that use PHP SOAP functions acceptable ? The complete documentation about call SOAP via PHP is here :
    http://php.net/manual/en/soapclient.soapcall.php

    You can read more references about PHP and SOAP here :
    http://wordpress.stackexchange.com/questions/14804/embedding-a-soap-client-into-a-wordpress-plugin

    And if you want to use wordpress native functions it is wordpress_remote_post since SOAP didn't support GET method. And you must pass your SOAP envelope in $args['body'] parameter. Example of the POST parameter you can see it here :
    http://stackoverflow.com/questions/471115/how-to-post-soap-request-from-php (second answer)

    But I recommed to go with PHP function instead call it through HTTP POST.

    Let me know what do you think :slight_smile:

    Best Regards

  • pcs
    • WPMU DEV Initiate

    Hi Anang,

    Creating a a new plugin/functions that use PHP SOAP functions is acceptable, though I would have liked to have used the native wp_remote_post. Either way I will be building the functionality into a plugin specific to the service I am consuming.

    Note the service we consume is usually just set to RPC, though I have enabled GET and POST as well.

    In trying to use wp_remote_post, I am having issues with the headers 'Content-Type'. I have seen examples which use application/x-www-form-urlencoded, text/xml and text/plain. I suspect the service I am consuming is expecting XML but not sure if this means the Content-Type should be text/xml?

    Why aren't you recommending wp_remote_post?

    Best regards

  • Anang
    • New Recruit

    Hi @Peter

    It's possible if I can test your SOAP server ? so I can give you real code example to access it ?

    I prefer to PHP SOAP call functions because it's simpler to call then using HTTP POST method, which you must create XML based SOAP request to access your SOAP. In PHP SOAP call functions, you can call SOAP function just like call a function in a Class with parameter.

    Let me know if I can get the access :slight_smile:

    Best Regards

  • pcs
    • WPMU DEV Initiate

    Hi Anang,

    I took your advice and was able to get it going with one simple line!

    $client = new SoapClient($url, array('soap_version' => SOAP_1_2, 'trace' => 1, 'exceptions' => false, 'cache_wsdl' => WSDL_CACHE_NONE ) );

    I could then call the method directly (or optionally __soapCall). For example I have simple Ping method:

    $result = $client->ping('TESTING 123');

    Works perfectly.

    Thank you again for your assistance. Joining the community has certainly paid off already.

    Cheers
    Peter

  • pcs
    • WPMU DEV Initiate

    Hi Anang,

    I spoke too soon. I am consuming a RPC encoded SOAP 1.2 wsdl. Although I can access the service by importing (referencing) the wsdl file directly (on the server) I am getting a SoapFault:

    [message:protected] => encodingStyle cannot be specified on the Body

    I have to assume I have a well formed wsdl as it is auto generated (from our legacy system).

    Do I have to set something on the SoapClient header (SoapHeader)?

    Regards,
    Peter

  • Anang
    • New Recruit

    Hi @Peter

    Try to add encoding parameter on your SOAP Client constructor.
    $client = new SoapClient($url, array('soap_version' => SOAP_1_2, 'trace' => 1, 'exceptions' => false, 'cache_wsdl' => WSDL_CACHE_NONE,'encoding'=>'ISO-8859-1' ) );

    Let me know the result :slight_smile:

    Best Regards

  • pcs
    • WPMU DEV Initiate

    Hi Anang,

    Sadly this doesn't change the outcome. I had researched this and tested this already (and again after your post).

    I tested a Literal/Document (non-encoded) version of the service (WSDL) and it works perfectly. If I can't get the encoded version to work I may resort to running both versions (this is achievable).

    Any further suggestions would be appreciated as I may need to consume encoded services in the future.

    Best regards,

  • Anang
    • New Recruit

    Hi @Peter

    After looking some references I found out that you must use SOAP 1.1 if you want to use encoded version :
    http://forums.ocsinventory-ng.org/viewtopic.php?id=10329

    Or you can use something like binding SOAP 1.1 function to SOAP 1.2 as described on this article :

    Notice that SoapClient used SOAP 1.2 namespaces correctly in the SOAP request.
    This confirms that SOAP Extension in PHP 5.3.1 does support RPC method based Web services defined in WSDL 1.1 with SOAP 1.2 binding.

    http://www.herongyang.com/WSDL/SOAP-12-Client-PHP-SOAP-Extension-RPC-Method.html

    Let me know what's your opinion :slight_smile:

    Best Regards

  • pcs
    • WPMU DEV Initiate

    Hi @Anang,

    Thank you for the additional references. As the current service is SOAP 1.2 encoded and cannot be changed, I have created a parallel SOAP 1.2 literal service and will expose both.

    Given what you say, I need to either expose a SOAP 1.1 encoded or SOAP 1.2 literal to WordPress. I have chosen the latter option at this stage.

    Thank you for all your assistance.

    Best regards

  • Nino
    • New Recruit

    I'm having trouble installing SOAP API to our WordPress Website and I'm glad to see that there is fix here. Is it possible for you to share the steps on how I can do the same solution to mine? Thank you in advance. I'd greatly appreciate your help.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.