OK, här kommer en komplett webblösning där man kan skicka SMS via sms.inleed.se och deras sms-API.
autoload.php ska ligga i en underkatalog som heter 'lib'.
PHP-kod:
<?php
// File: autoload.php
// Modified by Conny Westh 2012-09-13
function __autoload($class_name)
{
if(file_exists($class_name . '.php'))
{
require_once($class_name . '.php');
}
else
{
throw new Exception("Kan inte ladda klass: $class_name.");
}
}
?>
SmsUtility.php är klassen som anropar API-funktionen hos sms.inleed.se.
PHP-kod:
<?php
////////////////////////////////////////////////////////////////////////////////
// File: SmsUtility.php
// Date: 2013-07-21 // created file_get_contents()
// Date: 2013-07-23 // added cURL...
// Author: Conny Westh, Adopter KB, 073-898 68 61, [email protected]
// UTF-8 med URLEncode
////////////////////////////////////////////////////////////////////////////////
// Refgistrera dig och skaffa en sms-nyckel på http://sms.inleed.se
// Alla SMS skickas idag från nummer 0763448100.
////////////////////////////////////////////////////////////////////////////////
class SmsUtility
{
var $smskey;
function __construct($smskey)
{
$this->smskey = $smskey;
}
function __destruct()
{
}
function send($recipient, $message)
{
$encodedMessage = $this->url_encode($message);
$url = "http://sms.inleed.se/skickaSMS/?nummer=" . $recipient . "&text=" . $encodedMessage . "&nyckel=" . $this->smskey;
// echo $url . "\n";
try
{
$this->curl_execute($url); // Using cURL...
// $this->file_execute($url); // Using file_get_contents()...
}
catch (Exception $e)
{
echo $e;
throw new Exception($e);
}
}
// To use cURL on Windows-platform you need to
// copy libeay32.dll and ssleay32.dll to Windows\System32
// and uncomment ;php_libcurl.dll in php.ini, the maybe reboot PC...
function curl_execute($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_exec($ch);
curl_close($ch);
}
function file_execute($url)
{
file_get_contents($url);
}
function url_encode($string)
{
return rawurlencode(utf8_encode($string));
}
function url_decode($string)
{
return utf8_decode(rawurldecode($string));
}
}
?>
Webbsidan SmsForm.php för att testa applikationen. Man behöver först skaffa en sms-nyckel från
http://sms.inleed.se för att köra detta. SMS-Nyckeln matar man in i det första fältet på webbsidan. Men det är ganska självförklarande när man väl ser webbsidan.
PHP-kod:
<html>
<body>
<?php
////////////////////////////////////////////////////////////////////////////////
// File: SmsForm.php
// Date: 2013-07-24 // created file_get_contents()
// Author: Conny Westh, Adopter KB, 073-898 68 61, [email protected]
////////////////////////////////////////////////////////////////////////////////
// Använder SmsUtility.php och /lib/autoload.php
// För att starta PHPs inbyggda webserver som localhost på port 8000
// så kör man "PHP -S localhost:8000" på kommandoraden. Sen kan man köra
// PHP SmsForm.php och testköra applikationen.
////////////////////////////////////////////////////////////////////////////////
// Registrera dig och skaffa en sms-nyckel på http://sms.inleed.se
// Alla SMS skickas idag från nummer 0763448100.
////////////////////////////////////////////////////////////////////////////////
require_once('/lib/autoload.php');
$debug=0;
$recipientPhoneNumber = $_REQUEST['recipient'];
$textMessage = $_REQUEST['message'];
$smsKey = $_REQUEST['smsKey'];
if ($debug>=1)
{
print '<br>';
print 'Mobilnummer: [';
print ($recipientPhoneNumber);
print ']<br>\n';
print 'Meddelande: [';
print ($textMessage);
print ']<br>\n';
print '<br>';
}
// Skicka sms...
try
{
$sms = new SmsUtility($smsKey);
$sms->send($recipientPhoneNumber, $textMessage);
echo "Meddelande: [" . $textMessage . "] skickat till [" . $recipientPhoneNumber . "].<br>\n";
}
catch (Exception $e)
{
echo "Error: " . $e->getMessage() . "<br>\n";
}
?>
<h1>Skicka SMS via sms.inleed.se</h1>
<!-- <form action=selfpost.php method=POST> -->
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<p>
SMS Key (ca 50 tecken): <input type=text name=smsKey value="<?php echo $smsKey ?>" size=70 maxlength=200></p>
<p>
Mottagare (mobilnummer 073...): <input type=text name=recipient value="<?php echo $recipientPhoneNumber ?>" size=15 maxlength=15></p>
<p>
Meddelande: <input type=text name=message value="<?php echo $textMessage ?>" size=160 maxlength=160></p>
<p>
<input type="submit" value="Skicka"></p>
</form>
<?php
?>
</body>
</html>