If there is an error, we will get "ERR: Error number". Let us write the function
now. The following code shows the implementation.
public function Request($command, $params)
{
$url = $this->apiURL.$command.'?';
// Add the session ID to requests
if ($command != "auth" && $this->sessionId != "")
{
$params['session_id'] = $this->sessionId;
}
foreach($params as $key=>$value)
{
$url .= "&$key=".urlencode($value);
}
try
{
// PHP's file() function can make HTTP GET requests and
// return the response
// So let's just use that for now
$response = file($url);
$resultArr = explode(":", $response[0]);
$this->lastResult = trim($resultArr[1]);
if ($resultArr[0] == "ERR")
{
Sending Text Messages
[ 106 ]
$this->lastResult = "ERR";
return false;
}
else
{
switch($command)
{
case "auth":
$this->sessionId = $this->lastResult;
break;
default:
break;
}
return true;
}
}
catch (Exception $ex)
{
// Problem, could not process the request
$this->lastResult = "ERR";
return false;
}
}
3. Now that the basics are in place, let us write a function to make a "Send"
request. We need the number to send the message to, the "from" number,
and the actual message.
Pages:
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149