MartinJJ MartinJJ - 7 days ago 4x
PHP Question

Initialise class and having problems with undefined offset

Im recently moving across from procedural to oop with my php, im studying from a book and as well as doing the exercises and projects in the book i am also giving myself a real life project to do for my own website.

I want to implement an sms feature to my website and while researching the project came across this small class library that connects to the API of the company i will be using, the library is on github and seems to be unsupported and my emails to the developer are bouncing back.

This is the library and this is the code i use to initialise it:


$username = "someuser";
$password = "somepass";

$api = new SmsQuick($username, $password);

//uncommenting the line below returns undefined_index @ line 312 and 282
//$available_credits = $api->checkBalance();

//var_dump($api); //used for checking

These are the errors i am receiving:

Notice: Undefined offset: 1 in C:\websites\ooptuts\public\classes\class.smsquick.php on line 312

Warning: array_values() expects parameter 1 to be array, boolean given in C:\websites\ooptuts\public\classes\class.smsquick.php on line 282

lines 312 and 282 are pointed out in the next two methods, the final two methods are required so i have shown those also:

public function checkBalance() {
$vars = array(
'username' => $this->api_username,
'password' => $this->api_password,
'action' => 'balance',

$retval = $this->executeApiRequest($vars);
list(, $response) = array_values(reset($retval)); // line 282

return (int) $response;

* Helper method to execute an API request.
* @param array $vars
* Data to POST to SMS gateway API endpoint.
* @return array
* Response from SMS gateway.
public function executeApiRequest($vars) {
// Basic validation on the authentication details
foreach ($vars as $key => $value) {
switch ($key) {
case 'username':
case 'password':
if (empty($value)) {
throw new Exception('API username or password not specified.');

$data = $this->preparePostData($vars);
$retval = $this->executePostRequest($data);

list($status, $response) = explode(':', $retval); // line 312
if ($status == 'ERROR') {
throw new Exception(strtr('There was an error with this request: !error.', array('!error' => $response)));

$data = array();
$lines = explode("\n", $retval);
foreach (array_filter($lines) as $i => $line) {
$line = trim($line);
$data[$i] = explode(':', $line);

return $data;

protected function preparePostData($data) {
$post_data = array();
foreach ($data as $key => $value) {
switch ($key) {
case 'to':
// Support multiple phone numbers.
$value = implode(',', array_unique($value));
$post_data[] = $key . '=' . rawurlencode($value);

return implode('&', $post_data);

protected function executePostRequest($data) {
$ch = curl_init($this->api_endpoint);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$retval = curl_exec($ch);
return $retval;

Are the problems caused by how i am initialising the class or something that i am overlooking?.. i seem to have spent two days running around in circles with google etc and not making much progress and would appreciate some advice from others.



Because reset() returns a Boolean. Move the reset() call to its own line:

list(, $response) = array_values($retval); // line 282

I should say it returns the first value, however in your case its returning FALSE

From the PHP Manual: reset()

Also, its possible that on line 312 the array you are passing in is empty. Try printing out the value of the array to be sure. Perhaps your CURL call is not returning what you expect.