PHP Call Web Service WSDL Example
Posted by in PHP July 26, 2011 6 Comments

I will show you a simple example about How to use PHP to call an API Web service.

For a demonstration, I use the MessageNetAPI which allows you to send an SMS one way via the MessageNet SMS Gateway.

1. .NET/SOAP information

  • Location: www.messagenet.com.au/dotnet
  • Function: LodgeSMSMessage
  • Description: Lodge SMS message into the MessageNet system. Phonenumber may be nnnn;nnnn;nnnn for multiple messages
  • Arguments:
    • username as string
    • pwd as string
    • phonenumber as string
    • phonemessage as string
  • Returns: <string xmlns=”http://www.messagenet.com.au/dotnet”>Message sent successfully.</string>. Anything else is error.
  • Example: LodgeSMSMessage(user1,password1,61412123456,Test MessageNet)

2. SOAP 1.2 request and response.

Request:

POST /dotnet/lodge.asmx HTTP/1.1
Host: www.messagenet.com.au
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
 
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <LodgeSMSMessage xmlns="http://www.messagenet.com.au/dotnet">
      <Username>string</Username>
      <Pwd>string</Pwd>
      <PhoneNumber>string</PhoneNumber>
      <PhoneMessage>string</PhoneMessage>
    </LodgeSMSMessage>
  </soap12:Body>
</soap12:Envelope>

Response:

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
 
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <LodgeSMSMessageResponse xmlns="http://www.messagenet.com.au/dotnet">
      <LodgeSMSMessageResult>string</LodgeSMSMessageResult>
    </LodgeSMSMessageResponse>
  </soap12:Body>
</soap12:Envelope>

3. PHP call MessageNet API Web Service

<?php
	$client = new SoapClient("http://www.messagenet.com.au/dotnet/Lodge.asmx?WSDL");
 
	$phone = "your-mobile-number"; //include country code and area code
 
	$message = "Your SMS message here ...";
 
	$result = $client->LodgeSMSMessage(array(
		"Username" => "your-username",
		"Pwd" => "your-password",
		"PhoneNumber" => $phone,
		"PhoneMessage" => $message
	));
 
	$response_arr = objectToArray($result);
 
	echo "return_code= " . str_replace(";", "", $response_arr["LodgeSMSMessageResult"]);
 
	function objectToArray($d)
	{
		if (is_object($d))
		{
			$d = get_object_vars($d);
		}
 
		if (is_array($d))
		{
			return array_map(__FUNCTION__, $d);
		}
		else
		{
			return $d;
		}
	}
?>

Note:

Download the PHP call api web service source code.

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com
  • http://www.cattechnologies.com/offshoreNETDevelopment.aspx Victor

    Nice article for PHP developers.

  • http://www.biztechconsultancy.com/ Web Development Company

    Nice blog created by the blog owner I like this post. It is very informative and attractive also I would like to come at this blog again and again.

  • don

    Thanks for this, the objectToArray function saved me from hours of frustration!

  • http://www.facebook.com/shabazch Shahbaz Chaudhary

    Hi, this is showing me Error : Uncaught SoapFault exception:

  • kaushik

    hi, i have a account on orange M2M connect.. I want to send and receive and store sms through this sms gatway..plz help me

  • http://suryarpraveen.WordPress.com/ Surya R Praveen

    Really thanks for this, it took just few seconds to figure it out from your code.