I'm getting API response in plain text. I need to grab data from that text response and need to store them as variables.
API Calling:
$url="http://91.101.61.111:99/SendRequest/?mobile=9999999999&id=11011&reqref=501";
$request_timeout = 60;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $request_timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $request_timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$curl_error = curl_errno($ch);
curl_close($ch);
API response in plain text:
REQUEST ACCEPTED your ref=501 system_reference=BA01562
I need to grab data from the above plain text response as variables, like below:
$status = "REQUEST ACCEPTED";
$myref = "501";
$sysref = "BA01562";
I have tried:
$explode1 = explode(" ", $output);
$explode2 = explode("=", $explode1[3]);
$explode3 = explode("=", $explode1[4]);
$status = $explode1[0]." ".$explode1[1];
$myref = $explode2[1];
$sysref = $explode3[1];
I know this is not a proper way to do this. But I am not able to figure out the proper way to do it since I'm a newbie.
Please help! Thank you!
you can use a preg_match, something like:
$rc = preg_match('/([\w\s]+) your ref=([\d]+) system_reference=([\w]+)/', $plain_response, $matches);
if ($rc)
{
$status = $matches[1];
$myref = $matches[2];
$sysref = $matches[3];
}
but of course, just as @Don't panic said, you need a bit more knowledge of the API, to be sure about parsing. The example i gived is a bit childish. Anyway, when you will be sure about the format, use regexp with preg_match.