How to make a PHP SOAP call using the SoapClient class

后端 未结 12 769
星月不相逢
星月不相逢 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)

    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!

提交回复
热议问题