How to make a PHP SOAP call using the SoapClient class

后端 未结 12 566
星月不相逢
星月不相逢 2020-11-22 17:28

I\'m used to writing PHP code, but do not often use Object-Oriented coding. I now need to interact with SOAP (as a client) and am not able to get the syntax right. I\'ve got

相关标签:
12条回答
  • 2020-11-22 17:45

    This is what you need to do.

    I tried to recreate the situation...


    • For this example, I created a .NET sample WebService (WS) with a WebMethod called Function1 expecting the following params:

    Function1(Contact Contact, string description, int amount)

    • Where Contact is just a model that has getters and setters for id and name like in your case.

    • You can download the .NET sample WS at:

    https://www.dropbox.com/s/6pz1w94a52o5xah/11593623.zip


    The code.

    This is what you need to do at PHP side:

    (Tested and working)

    <?php
    // Create Contact class
    class Contact {
        public function __construct($id, $name) 
        {
            $this->id = $id;
            $this->name = $name;
        }
    }
    
    // Initialize WS with the WSDL
    $client = new SoapClient("http://localhost:10139/Service1.asmx?wsdl");
    
    // Create Contact obj
    $contact = new Contact(100, "John");
    
    // Set request params
    $params = array(
      "Contact" => $contact,
      "description" => "Barrel of Oil",
      "amount" => 500,
    );
    
    // Invoke WS method (Function1) with the request params 
    $response = $client->__soapCall("Function1", array($params));
    
    // Print WS response
    var_dump($response);
    
    ?>
    

    Testing the whole thing.

    • If you do print_r($params) you will see the following output, as your WS would expect:

    Array ( [Contact] => Contact Object ( [id] => 100 [name] => John ) [description] => Barrel of Oil [amount] => 500 )

    • When I debugged the .NET sample WS I got the following:

    enter image description here

    (As you can see, Contact object is not null nor the other params. That means your request was successfully done from PHP side)

    • The response from the .NET sample WS was the expected one and this is what I got at PHP side:

    object(stdClass)[3] public 'Function1Result' => string 'Detailed information of your request! id: 100, name: John, description: Barrel of Oil, amount: 500' (length=98)


    Happy Coding!

    0 讨论(0)
  • 2020-11-22 17:53

    You need declare class Contract

    class Contract {
      public $id;
      public $name;
    }
    
    $contract = new Contract();
    $contract->id = 100;
    $contract->name = "John";
    
    $params = array(
      "Contact" => $contract,
      "description" => "Barrel of Oil",
      "amount" => 500,
    );
    

    or

    $params = array(
      $contract,
      "description" => "Barrel of Oil",
      "amount" => 500,
    );
    

    Then

    $response = $client->__soapCall("Function1", array("FirstFunction" => $params));
    

    or

    $response = $client->__soapCall("Function1", $params);
    
    0 讨论(0)
  • 2020-11-22 17:54

    First initialize webservices:

    $client = new SoapClient("http://example.com/webservices?wsdl");
    

    Then set and pass the parameters:

    $params = array (
        "arg0" => $contactid,
        "arg1" => $desc,
        "arg2" => $contactname
    );
    
    $response = $client->__soapCall('methodname', array($params));
    

    Note that the method name is available in WSDL as operation name, e.g.:

    <operation name="methodname">
    
    0 讨论(0)
  • 2020-11-22 17:58

    You can use SOAP services this way too:

    <?php 
    //Create the client object
    $soapclient = new SoapClient('http://www.webservicex.net/globalweather.asmx?WSDL');
    
    //Use the functions of the client, the params of the function are in 
    //the associative array
    $params = array('CountryName' => 'Spain', 'CityName' => 'Alicante');
    $response = $soapclient->getWeather($params);
    
    var_dump($response);
    
    // Get the Cities By Country
    $param = array('CountryName' => 'Spain');
    $response = $soapclient->getCitiesByCountry($param);
    
    var_dump($response);
    

    This is an example with a real service, and it works.

    Hope this helps.

    0 讨论(0)
  • 2020-11-22 17:58

    I had the same issue, but I just wrapped the arguments like this and it works now.

        $args = array();
        $args['Header'] = array(
            'CustomerCode' => 'dsadsad',
            'Language' => 'fdsfasdf'
        );
        $args['RequestObject'] = $whatever;
    
        // this was the catch, double array with "Request"
        $response = $this->client->__soapCall($name, array(array( 'Request' => $args )));
    

    Using this function:

     print_r($this->client->__getLastRequest());
    

    You can see the Request XML whether it's changing or not depending on your arguments.

    Use [ trace = 1, exceptions = 0 ] in SoapClient options.

    0 讨论(0)
  • 2020-11-22 18:00

    read this;-

    http://php.net/manual/en/soapclient.call.php

    Or

    This is a good example, for the SOAP function "__call". However it is deprecated.

    <?php
        $wsdl = "http://webservices.tekever.eu/ctt/?wsdl";
        $int_zona = 5;
        $int_peso = 1001;
        $cliente = new SoapClient($wsdl);
        print "<p>Envio Internacional: ";
        $vem = $cliente->__call('CustoEMSInternacional',array($int_zona, $int_peso));
        print $vem;
        print "</p>";
    ?>
    
    0 讨论(0)
提交回复
热议问题