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:
- We’re using PHP SoapClient to call the API Web Service, so you need to enable the libxml PHP extension.
- As its response is an object (not string) so you need to convert it to an array by using objectToArray function.
Download the PHP call api web service source code.