How to easily consume a web service from PHP

PhpWeb ServicesVisual StudioWsdl

Php Problem Overview


Is there available any tool for PHP which can be used to generate code for consuming a web service based on its WSDL? Something comparable to clicking "Add Web Reference" in Visual Studio or the Eclipse plugin which does the same thing for Java.

Php Solutions


Solution 1 - Php

In PHP 5 you can use SoapClient on the WSDL to call the web service functions. For example:

$client = new SoapClient("some.wsdl");

and $client is now an object which has class methods as defined in some.wsdl. So if there was a method called getTime in the WSDL then you would just call:

$result = $client->getTime();

And the result of that would (obviously) be in the $result variable. You can use the __getFunctions method to return a list of all the available methods.

Solution 2 - Php

I've had great success with wsdl2php. It will automatically create wrapper classes for all objects and methods used in your web service.

Solution 3 - Php

I have used NuSOAP in the past. I liked it because it is just a set of PHP files that you can include. There is nothing to install on the web server and no config options to change. It has WSDL support as well which is a bonus.

Solution 4 - Php

This article explains how you can use PHP SoapClient to call a api web service.

Solution 5 - Php

Say you were provided the following:

<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:int="http://thesite.com/">
    <x:Header/>
    <x:Body>
        <int:authenticateLogin>
            <int:LoginId>12345</int:LoginId>
        </int:authenticateLogin>
    </x:Body>
</x:Envelope>

and

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <authenticateLoginResponse xmlns="http://thesite.com/">
            <authenticateLoginResult>
                <RequestStatus>true</RequestStatus>
                <UserName>003p0000006XKX3AAO</UserName>
                <BearerToken>Abcdef1234567890</BearerToken>
            </authenticateLoginResult>
        </authenticateLoginResponse>
    </s:Body>
</s:Envelope>

Let's say that accessing http://thesite.com/ said that the WSDL address is: http://thesite.com/PortalIntegratorService.svc?wsdl

$client = new SoapClient('http://thesite.com/PortalIntegratorService.svc?wsdl');
$result = $client->authenticateLogin(array('LoginId' => 12345));
if (!empty($result->authenticateLoginResult->RequestStatus)
    && !empty($result->authenticateLoginResult->UserName)) {
    echo 'The username is: '.$result->authenticateLoginResult->UserName;
}

As you can see, the items specified in the XML are used in the PHP code though the LoginId value can be changed.

Solution 6 - Php

Well, those features are specific to a tool that you are using for development in those languages.

You wouldn't have those tools if (for example) you were using notepad to write code. So, maybe you should ask the question for the tool you are using.

For PHP: <http://webservices.xml.com/pub/a/ws/2004/03/24/phpws.html>

Solution 7 - Php

HI I got this from this site : http://forums.asp.net/t/887892.aspx?Consume+an+ASP+NET+Web+Service+with+PHP

The web service has method Add which takes two params:

<?php
    $client = new SoapClient("http://localhost/csharp/web_service.asmx?wsdl");

     print_r( $client->Add(array("a" => "5", "b" =>"2")));
?>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEdoView Question on Stackoverflow
Solution 1 - PhpdavidmyttonView Answer on Stackoverflow
Solution 2 - Phppix0rView Answer on Stackoverflow
Solution 3 - PhpWally LawlessView Answer on Stackoverflow
Solution 4 - PhpHoanView Answer on Stackoverflow
Solution 5 - PhpLuke WenkeView Answer on Stackoverflow
Solution 6 - PhpVaibhavView Answer on Stackoverflow
Solution 7 - PhpMashudu MuleyaView Answer on Stackoverflow