0

I'm using BulkSMS which is a SMS service and I want to get how many messages I have left using the URL response.

This is the URL (with password removed): https://www.bulksms.co.uk/eapi/user/get_credits/1/1.1?username=<username>&password=<password>

This then outputs something similar to: 0|2000.00

According to the documentation the first part refers to error messages and the second part refers to no. of messages remaining: status_code|status_description

So using cURL and explode I can get the URL response split via an array, but my question is how do I output the 2000.00 (status_description) as I would only want the 0 (status_code) for error checking?

function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    $array = explode('|', $data);
    print_r($array);
}

If it helps this is what the function outputs: Array ( [0] => 0 [1] => 2000.00 )

Also I know I could use substr and strpos to get the remaining messages as shown below, but I would like to use the status_code for error checking.

$remaining_sms = substr($data, strpos($data, "|") + 1);    
echo $remaining_sms;
2
  • 2
    Ummm... echo $array[0]; and echo $array[1]; Commented Jun 13, 2017 at 20:22
  • Ha well I feel stupid! Commented Jun 13, 2017 at 20:26

1 Answer 1

2

If you just want the second part of the pipe delimited message then just return that part of the array, given you know the index...

This will output what you want

function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    $array = explode('|', $data);
    print_r($array[1]);
}

Or alternatively you could return it and then parse it in your code later

function get_data($url) {
        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $data = curl_exec($ch);
        curl_close($ch);
        $array = explode('|', $data);
        return $array;
    }

Either way I don't get why you're substring-ing when you've already gotten it

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.