Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save MustafaMagdi/b7707b0577a215b51fb316daa946a60d to your computer and use it in GitHub Desktop.

Select an option

Save MustafaMagdi/b7707b0577a215b51fb316daa946a60d to your computer and use it in GitHub Desktop.

Revisions

  1. @umidjons umidjons revised this gist Dec 18, 2014. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions web-service-soap-client-server-php.md
    Original file line number Diff line number Diff line change
    @@ -71,6 +71,11 @@ $resp =$client->bookYear($book);
    var_dump($resp);
    ```

    Dump of the response:
    ```
    string '2012' (length=4)
    ```

    WSDL file `test.wsdl`:
    ```xml
    <?xml version="1.0" encoding="UTF-8"?>
  2. @umidjons umidjons revised this gist Dec 18, 2014. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions web-service-soap-client-server-php.md
    Original file line number Diff line number Diff line change
    @@ -49,6 +49,28 @@ $server->addFunction('bookYear');
    $server->handle();
    ```

    Implementation of the SOAP client - `client.php`:
    ```php
    <?php
    // model
    class Book
    {
    public $name;
    public $year;
    }

    // create instance and set a book name
    $book =new Book();
    $book->name='test 2';

    // initialize SOAP client and call web service function
    $client=new SoapClient('http://a.uz/soap/server.php?wsdl',['trace'=>1,'cache_wsdl'=>WSDL_CACHE_NONE]);
    $resp =$client->bookYear($book);

    // dump response
    var_dump($resp);
    ```

    WSDL file `test.wsdl`:
    ```xml
    <?xml version="1.0" encoding="UTF-8"?>
  3. @umidjons umidjons revised this gist Dec 18, 2014. 1 changed file with 60 additions and 0 deletions.
    60 changes: 60 additions & 0 deletions web-service-soap-client-server-php.md
    Original file line number Diff line number Diff line change
    @@ -47,4 +47,64 @@ $server->addFunction('bookYear');

    // start handling requests
    $server->handle();
    ```

    WSDL file `test.wsdl`:
    ```xml
    <?xml version="1.0" encoding="UTF-8"?>

    <wsdl:definitions name="Library"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="Library"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:tns="Library"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

    <xsd:documentation></xsd:documentation>

    <wsdl:types>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="Library">
    <xsd:complexType name="book">
    <xsd:sequence>
    <xsd:element name="name" type="xsd:string"></xsd:element>
    <xsd:element name="year" type="tns:integer"></xsd:element>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:schema>
    </wsdl:types>

    <wsdl:message name="bookYearRequest">
    <wsdl:part name="book" type="xsd:book"></wsdl:part>
    </wsdl:message>
    <wsdl:message name="bookYearResponse">
    <wsdl:part name="year" type="tns:integer"></wsdl:part>
    </wsdl:message>

    <wsdl:portType name="Library">
    <wsdl:operation name="bookYear">
    <wsdl:input message="tns:bookYearRequest"/>
    <wsdl:output message="tns:bookYearResponse"/>
    </wsdl:operation>
    </wsdl:portType>

    <wsdl:binding name="Library" type="tns:Library">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="bookYear">
    <soap:operation soapAction="http://a.uz/soap/server.php"/>
    <wsdl:input>
    <soap:body use="literal" namespace="Library"/>
    </wsdl:input>
    <wsdl:output>
    <soap:body use="literal" namespace="Library"/>
    </wsdl:output>
    </wsdl:operation>
    </wsdl:binding>

    <wsdl:service name="Library">
    <wsdl:port binding="tns:Library" name="BookLibrary">
    <soap:address location="http://a.uz/soap/server.php"/>
    </wsdl:port>
    </wsdl:service>

    </wsdl:definitions>
    ```
  4. @umidjons umidjons created this gist Dec 18, 2014.
    50 changes: 50 additions & 0 deletions web-service-soap-client-server-php.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    Simple Web service - SOAP Server/Client in PHP
    ===

    Implementation of the SOAP server - `server.php`:
    ```php
    <?php
    // turn off WSDL caching
    ini_set("soap.wsdl_cache_enabled","0");

    // model, which uses in web service functions as parameter
    class Book
    {
    public $name;
    public $year;
    }

    /**
    * Determines published year of the book by name.
    * @param Book $book book instance with name set.
    * @return int published year of the book or 0 if not found.
    */
    function bookYear($book)
    {
    // list of the books
    $_books=[
    ['name'=>'test 1','year'=>2011],
    ['name'=>'test 2','year'=>2012],
    ['name'=>'test 3','year'=>2013],
    ];
    // search book by name
    foreach($_books as $bk)
    if($bk['name']==$book->name)
    return $bk['year']; // book found

    return 0; // book not found
    }

    // initialize SOAP Server
    $server=new SoapServer("test.wsdl",[
    'classmap'=>[
    'book'=>'Book', // 'book' complex type in WSDL file mapped to the Book PHP class
    ]
    ]);

    // register available functions
    $server->addFunction('bookYear');

    // start handling requests
    $server->handle();
    ```