Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ Require the Clockwork library:
require 'class-Clockwork.php';

### Sending a message

$clockwork = new Clockwork( $API_KEY );
$clockwork = new \Clockwork\Clockwork( $API_KEY );
$message = array( 'to' => '441234567891', 'message' => 'This is a test!' );
$result = $clockwork->send( $message );

### Sending multiple messages

We recommend you use batch sizes of 500 messages or fewer. By limiting the batch size it prevents any timeouts when sending.

$clockwork = new Clockwork( $API_KEY );
$clockwork = new \Clockwork\Clockwork( $API_KEY );
$messages = array(
array( 'to' => '441234567891', 'message' => 'This is a test!' ),
array( 'to' => '441234567892', 'message' => 'This is a test 2!' )
Expand Down Expand Up @@ -103,7 +102,7 @@ For example, if you send to invalid phone number "abc":

Check your available SMS balance:

$clockwork = new Clockwork( $API_KEY );
$clockwork = new \Clockwork\Clockwork( $API_KEY );
$clockwork->checkBalance();

This will return:
Expand All @@ -121,11 +120,11 @@ The Clockwork wrapper will throw a `ClockworkException` if the entire call faile

try
{
$clockwork = new Clockwork( 'invalid_key' );
$clockwork = new \Clockwork\Clockwork( 'invalid_key' );
$message = array( 'to' => 'abc', 'message' => 'This is a test!' );
$result = $clockwork->send( $message );
}
catch( ClockworkException $e )
catch( \Clockwork\ClockworkException $e )
{
print $e->getMessage();
// Invalid API Key
Expand Down Expand Up @@ -183,7 +182,7 @@ Set option values individually on each message.

In this example, one message will be from Clockwork and the other from 84433:

$clockwork = new Clockwork( $API_KEY, $options );
$clockwork = new \Clockwork\Clockwork( $API_KEY, $options );
$messages = array(
array( 'to' => '441234567891', 'message' => 'This is a test!', 'from' => 'Clockwork' ),
array( 'to' => '441234567892', 'message' => 'This is a test 2!', 'from' => '84433' )
Expand All @@ -207,7 +206,7 @@ If you're seeing this error there are two fixes available, the first is easy, si
#### Disable SSL on Clockwork calls

$options = array( 'ssl' => false );
$clockwork = new Clockwork( $API_KEY, $options );
$clockwork = new \Clockwork\Clockwork( $API_KEY, $options );

#### Setup SSL root certificates on your server

Expand Down
12 changes: 12 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "weemen/clockwork-php",
"minimum-stability":"dev",
"require-dev": {
"phpunit/phpunit": "3.7.*"
},
"autoload": {
"psr-0": {
"Clockwork\\": "src/"
}
}
}
31 changes: 15 additions & 16 deletions class-Clockwork.php → src/Clockwork/Clockwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
* @version 1.3.0
*/

if ( !class_exists('ClockworkException') ) {
require_once('class-ClockworkException.php');
}
namespace Clockwork;
use ClockworkException;

/**
* Main Clockwork API Class
Expand Down Expand Up @@ -173,7 +172,7 @@ public function send(array $sms) {
$sms = array($sms);
}

$req_doc = new DOMDocument('1.0', 'UTF-8');
$req_doc = new \DOMDocument('1.0', 'UTF-8');
$root = $req_doc->createElement('Message');
$req_doc->appendChild($root);

Expand Down Expand Up @@ -251,7 +250,7 @@ public function send(array $sms) {
$req_xml = $req_doc->saveXML();

$resp_xml = $this->postToClockwork(self::API_SMS_METHOD, $req_xml);
$resp_doc = new DOMDocument();
$resp_doc = new \DOMDocument();
$resp_doc->loadXML($resp_xml);

$response = array();
Expand Down Expand Up @@ -317,7 +316,7 @@ public function send(array $sms) {
*/
public function checkCredit() {
// Create XML doc for request
$req_doc = new DOMDocument('1.0', 'UTF-8');
$req_doc = new \DOMDocument('1.0', 'UTF-8');
$root = $req_doc->createElement('Credit');
$req_doc->appendChild($root);
$root->appendChild($req_doc->createElement('Key', $this->key));
Expand All @@ -327,7 +326,7 @@ public function checkCredit() {
$resp_xml = $this->postToClockwork(self::API_CREDIT_METHOD, $req_xml);

// Create XML doc for response
$resp_doc = new DOMDocument();
$resp_doc = new \DOMDocument();
$resp_doc->loadXML($resp_xml);

// Parse the response to find credit value
Expand Down Expand Up @@ -365,7 +364,7 @@ public function checkCredit() {
*/
public function checkBalance() {
// Create XML doc for request
$req_doc = new DOMDocument('1.0', 'UTF-8');
$req_doc = new \DOMDocument('1.0', 'UTF-8');
$root = $req_doc->createElement('Balance');
$req_doc->appendChild($root);
$root->appendChild($req_doc->createElement('Key', $this->key));
Expand All @@ -375,7 +374,7 @@ public function checkBalance() {
$resp_xml = $this->postToClockwork(self::API_BALANCE_METHOD, $req_xml);

// Create XML doc for response
$resp_doc = new DOMDocument();
$resp_doc = new \DOMDocument();
$resp_doc->loadXML($resp_xml);

// Parse the response to find balance value
Expand Down Expand Up @@ -426,7 +425,7 @@ public function checkBalance() {
*/
public function checkKey() {
// Create XML doc for request
$req_doc = new DOMDocument('1.0', 'UTF-8');
$req_doc = new \DOMDocument('1.0', 'UTF-8');
$root = $req_doc->createElement('Authenticate');
$req_doc->appendChild($root);
$root->appendChild($req_doc->createElement('Key', $this->key));
Expand All @@ -436,7 +435,7 @@ public function checkKey() {
$resp_xml = $this->postToClockwork(self::API_AUTH_METHOD, $req_xml);

// Create XML doc for response
$resp_doc = new DOMDocument();
$resp_doc = new \DOMDocument();
$resp_doc->loadXML($resp_xml);

// Parse the response to see if authenticated
Expand Down Expand Up @@ -525,9 +524,9 @@ protected function xmlPost($url, $data) {
$info = curl_getinfo($ch);

if ($response === false || $info['http_code'] != 200) {
throw new Exception('HTTP Error calling Clockwork API - HTTP Status: ' . $info['http_code'] . ' - cURL Erorr: ' . curl_error($ch));
throw new \Exception('HTTP Error calling Clockwork API - HTTP Status: ' . $info['http_code'] . ' - cURL Erorr: ' . curl_error($ch));
} elseif (curl_errno($ch) > 0) {
throw new Exception('HTTP Error calling Clockwork API - cURL Error: ' . curl_error($ch));
throw new \Exception('HTTP Error calling Clockwork API - cURL Error: ' . curl_error($ch));
}

curl_close($ch);
Expand All @@ -553,17 +552,17 @@ protected function xmlPost($url, $data) {
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
ini_set('track_errors',$track);
throw new Exception("HTTP Error calling Clockwork API - fopen Error: $php_errormsg");
throw new \Exception("HTTP Error calling Clockwork API - fopen Error: $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
ini_set('track_errors',$track);
throw new Exception("HTTP Error calling Clockwork API - stream Error: $php_errormsg");
throw new \Exception("HTTP Error calling Clockwork API - stream Error: $php_errormsg");
}
ini_set('track_errors',$track);
return $response;
} else {
throw new Exception("Clockwork requires PHP5 with cURL or HTTP stream support");
throw new \Exception("Clockwork requires PHP5 with cURL or HTTP stream support");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
* @copyright Mediaburst Ltd 2012
* @license ISC
* @link http://www.clockworksms.com
*/
*/

namespace Clockwork;

/*
* ClockworkException
Expand Down
26 changes: 26 additions & 0 deletions tests/Clockwork/ClockworkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Clockwork;
use \PHPUnit_Framework_TestCase;

class ClockworkTest extends PHPUnit_Framework_TestCase {

public function msisdProvider()
{
return array(
array('01234567', 0), //starts with 0
array('11234567', 1),
array('1123456', 0), //not enough numbers
array('1123456789000', 1),
array('11234567890000', 0) //too many numbers
);
}

/**
* @dataProvider msisdProvider
*/
public function testIsValidMsisd($number, $expectedResult)
{
$this->assertEquals($expectedResult,Clockwork::is_valid_msisdn($number));
}
}
2 changes: 2 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
require 'vendor/autoload.php';