Call an ASMX web service from PHP

If you need to call an ASMX web service with several parameter from an PHP environment you’ll need to pass those parameters in a specific way in order to get this working. Suppose we have a web service with a method GetProducts which requires three parameters, which is defined like:

[WebMethod]
public array[products] GetProducts(string SearchTerm, int Skip, int Take)

Most people will try to call this method from PHP with code that looks like:

$WSDL_URI = "https://api.company.com/Products.asmx?WSDL";
$client = new SoapClient($WSDL_URI);
$ProductData = $client->GetProducts($SearchFor,0,12);

And you’ll notice the this call fails with an error or it doesn’t fail, but it doesn’t return what you expect. The solution is remarkably simple, just put the parameters in a named array:

$WSDL_URI = "https://api.company.com/Products.asmx?WSDL";
$params = array("SearchTerm" => $SearchFor, "Skip" => 0, “Take” => 12);
$client = new SoapClient($WSDL_URI);
$ProductData = $client->GetProducts($params );

And then you’ll notice the you called the ASMX service from PHP and it works just like you expect.

Comments are closed.