Home > Backend Development > PHP Tutorial > How to Send Structured Data with PHP's SoapClient: A Step-by-Step Guide?

How to Send Structured Data with PHP's SoapClient: A Step-by-Step Guide?

Susan Sarandon
Release: 2024-12-11 16:53:13
Original
770 people have browsed it

How to Send Structured Data with PHP's SoapClient: A Step-by-Step Guide?

How to send structured data in a PHP SOAP call using SoapClient

When working with SOAP web services, it's essential to understand how to structure and send data in a format compatible with the service. This article demonstrates how to use the SoapClient class to make a SOAP call, specifically focusing on handling structured data.

In the scenario presented, our goal is to make a call to the "FirstFunction" function of a web service using the following structured data:

  • Contact (id, name)
  • General Description
  • Amount

Step-by-Step Guide

  1. Create a Contact Class (if necessary)

    For this example, we assume you have a PHP class named Contact with properties id and name. If you don't have this class, create it as follows:

    class Contact {
        public $id;
        public $name;
    }
    Copy after login
  2. Utilize SoapClient for SOAP Function Call

    a. Instantiate the SoapClient with the web service WSDL URL:

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

    b. Create an instance of the Contact class with the desired data:

    $contact = new Contact();
    $contact->id = 100;
    $contact->name = "John";
    Copy after login

    c. Set request parameters in the correct structure:

    $params = array(
        "Contact" => $contact,
        "description" => "Barrel of Oil",
        "amount" => 500,
    );
    Copy after login

    d. Invoke the SOAP method:

    $response = $client->__soapCall("Function1", array($params));
    Copy after login

    Note: Make sure to replace "Function1" with the actual name of your SOAP function.

By following these steps, you can successfully make SOAP calls using the SoapClient class and pass structured data to the web service as expected.

The above is the detailed content of How to Send Structured Data with PHP's SoapClient: A Step-by-Step Guide?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template