How to grab data from plain text response of API Calls in PHP? -
i'm getting api response in plain text. need grab data text response , need store them 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 ref=501 system_reference=ba01562     i need grab data above plain text response variables, 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 not proper way this. not able figure out proper way since i'm newbie.
please help! thank you!
you can use preg_match, like:
$rc = preg_match('/([\w\s]+) ref=([\d]+) system_reference=([\w]+)/', $plain_response, $matches); if ($rc) {     $status = $matches[1];       $myref = $matches[2];       $sysref = $matches[3];   } but of course, @don't panic said, need bit more knowledge of api, sure parsing. example gived bit childish. anyway, when sure format, use regexp preg_match.
Comments
Post a Comment