douwa6220 2017-09-14 15:16
浏览 45

如何从PHP中的两个可能的API文本响应中的一个响应中获取数据?

There's two possible responses of my API. I need to grab data from the received 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); 

Possible API responses in plain text:

REQUEST ACCEPTED your ref=<myref> system_reference=<sysref>
Or,
REQUEST ERROR errorno=<error>;your ref=<myref>;system_reason=<sysreason>

In case of 1st possible response, I need to grab data like below:

$status = "REQUEST ACCEPTED";  
$myref = "501";  
$sysref = "BA01562";  

And in case of 2nd possible response, I need to grab data like below:

$status = "REQUEST ERROR";
$error = "25";
$myref = "501";
$sysreason = "Duplicate request";  

I have tried:

$response = preg_match('/([\w\s]+) ([\w]+)/', $output, $res);
$rstatus = $res[1];

if ($rstatus == "REQUEST ACCEPTED")
{
    $raccepted = preg_match('/([\w\s]+) your ref=([\d]+) system_reference=([\w]+)/', $output, $matches);
    $status = $matches[1];  
    $myref = $matches[2];  
    $sysref = $matches[3];  
}
elseif ($rstatus == "REQUEST ERROR")
{
    $rerror = preg_match('/([\w\s]+) errorno=([\d]+);your ref=([\d]+);system_reason=([\w\s]+)/', $output, $matches);
    $status = $matches[1];
    $error = $matches[2]; 
    $myref = $matches[3];  
    $sysreason = $matches[4];  
}

echo "Status is $status, My Ref ID is $myref";

Now, when I get the 1st possible response from API call, I always get the errors on the last line (echo ....), like below:

( ! ) Notice: Undefined variable: status
( ! ) Notice: Undefined variable: myref
Status is , My Ref ID is

But no issues when I receive the 2nd response. It shows as I want:

Status is REQUEST ERROR, My Ref ID is 501

Please help!

  • 写回答

2条回答 默认 最新

  • doulai8128 2017-09-14 15:37
    关注

    This is not the most efficient method, try using some proper format like JSON or something. Anyway, here is a solution:

    $success = stristr($output, "REQUEST ACCEPTED");
    
    if($success)
    {
        $data = stristr($output, "ref");
        $raccepted = preg_match('/ref=([\d]+) system_reference=([\w]+)/', $data, $matches);
        var_dump($matches);
    }
    else
    {
        $data = stristr($output, "errorno");
        $rerror = preg_match('/errorno=([\d]+);your ref=([\d]+);system_reason=([\w\s]+)/', $data, $matches);
        var_dump($matches);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料