Created
March 10, 2012 08:54
-
-
Save jrmadsen67/2010885 to your computer and use it in GitHub Desktop.
called.in wrapper for Codeigniter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class Calledin{ | |
private $ci; | |
private $messages = array( | |
0=>'Success', | |
1=>'invalid api_key', | |
2=>'no call credits', | |
3=>'the phone_number is on the DoNotCall list', | |
4=>'badly formed request. make sure all parameters match the above descriptions', | |
5=>'api_key is currently disabled', | |
6=>'flood protection for phone_number. please wait 1 minute and try request again', | |
7=>'phone_number area code is not supported at this time', | |
8=>'country_code is not supported', | |
); | |
public function __construct() | |
{ | |
$this->ci = & get_instance(); | |
$this->ci->load->config('calledin'); | |
$this->calledin_config = array( | |
'api_url' => config_item('api_url'), | |
'api_key' => config_item('api_key'), | |
); | |
} | |
function verify($phone, $country_code =1, $code=0) | |
{ | |
$retval = array('opstat'=>1, 'msg_number'=>999 ,'msg'=>'Internal error'); | |
if (!$code) | |
{ | |
return $retval; //internal error. calledin return 0 = success | |
} | |
$api_url = $this->calledin_config['api_url']; | |
$api_key = $this->calledin_config['api_key']; | |
$phone = $this->_clean_phone_number($phone); | |
$code = $this->_clean_code($code); | |
if ($api_url && $api_key && $phone && $code) | |
{ | |
$query_array = array( | |
'api_key'=>$api_key, | |
'phone_number'=>$phone, | |
'country_code'=>$country_code, | |
'code'=>$code, | |
); | |
$query = http_build_query($query_array); | |
$url = $api_url.'?'.$query; | |
//issue the API call to Called.in | |
$return_code = file_get_contents($url); | |
if($return_code == 0) | |
{ | |
$retval['opstat'] = 0; | |
} | |
$retval['msg_number'] = $return_code; | |
$retval['msg'] = $this->messages[$retval['msg_number']]; | |
return $retval; | |
} | |
return $retval; | |
} | |
function _clean_phone_number($phone) | |
{ | |
$phone = preg_replace('/\D/', '', $phone); | |
if (strlen($phone) == 10) | |
{ | |
return $phone; | |
} | |
return false; | |
} | |
function _clean_code($code) | |
{ | |
$code = preg_replace('/\D/', '', $code); | |
if (strlen($code) > 0 && strlen($code) < 7) | |
{ | |
return $code; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment