Reputation: 589
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;
Upvotes: 0
Views: 301
Reputation: 182
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
Upvotes: 2