简体   繁体   中英

How to get proper JSON from MyOprators webhook response?

Below string you get from webhook response from MyOprators, now we need proper json for read webhook data.

you can check below php script for identify web hook response

$res = '--------------------------691661548534e236
Content-Disposition: form-data; name="myoperator"

{"_ai":"40dc080f277b85d0eaca7193db01a8576b341968","_so":"2","_ci":"5dba83337c5f6944","_cl":" 919304631696","_cr":"9304631696","_cm":"","_st":1574228759,"_ms":1574228759000,"_ss":20759,"_et":1574228759,"_ts":1574228775,"_drs":0,"_dr":"00:00:00","_drm":0,"_ty":1,"_ev":1,"_su":1,"_fu":"","_fn":"","_cn":null,"_pm":[{"ky":"ui","vl":"5dd4d327007c8641"},{"ky":"is","vl":"0"},{"ky":"vt","vl":"2"},{"ky":"ic","vl":"0"},{"ky":"ia","vl":"0"}],"_cy":"91","_se":"BR, IN","_ld":[{"_tx":"","_ty":1,"_dr":"00:00:00","_st":1574228759,"_et":1574228759,"_ac":"received","_rr":[{"_id":"5dbc27ad024dc844","_na":"Divya","_em":null,"_ex":"12","_ct":" 919871628174","_nr":"9871628174"}]}],"_us":[{"ky":"5dbc27ad024dc844","vl":"received"}],"_tc":[{"ye":"5dbc27ad024dc844","yf":0}]}
--------------------------691661548534e236--
';

//echo $res;



function getBetween($string, $start = "", $end = ""){
    if (strpos($string, $start)) { // required if $start not exist in $string
        $startCharCount = strpos($string, $start) + strlen($start);
        $firstSubStr = substr($string, $startCharCount, strlen($string));
        $endCharCount = strpos($firstSubStr, $end);
        if ($endCharCount == 0) {
            $endCharCount = strlen($firstSubStr);
        }
        return substr($firstSubStr, 0, $endCharCount);
    } else {
        return '';
    }
}
echo "<pre>";
echo $js = getBetween($res, 'name="myoperator"', "--------------------------");
$te = json_decode($js, true);
print_r($te);

I suggest using regexp.

I think that both the speed is extracted and the primitiveness of the logic will be more convenient for your problem solution.

Use this code.

$res = '--------------------------691661548534e236
Content-Disposition: form-data; name="myoperator"

{"_ai":"40dc080f277b85d0eaca7193db01a8576b341968","_so":"2","_ci":"5dba83337c5f6944","_cl":" 919304631696","_cr":"9304631696","_cm":"","_st":1574228759,"_ms":1574228759000,"_ss":20759,"_et":1574228759,"_ts":1574228775,"_drs":0,"_dr":"00:00:00","_drm":0,"_ty":1,"_ev":1,"_su":1,"_fu":"","_fn":"","_cn":null,"_pm":[{"ky":"ui","vl":"5dd4d327007c8641"},{"ky":"is","vl":"0"},{"ky":"vt","vl":"2"},{"ky":"ic","vl":"0"},{"ky":"ia","vl":"0"}],"_cy":"91","_se":"BR, IN","_ld":[{"_tx":"","_ty":1,"_dr":"00:00:00","_st":1574228759,"_et":1574228759,"_ac":"received","_rr":[{"_id":"5dbc27ad024dc844","_na":"Divya","_em":null,"_ex":"12","_ct":" 919871628174","_nr":"9871628174"}]}],"_us":[{"ky":"5dbc27ad024dc844","vl":"received"}],"_tc":[{"ye":"5dbc27ad024dc844","yf":0}]}
--------------------------691661548534e236--
';

// Getting json from string by regexp simple pattern
preg_match('/\{.*\}/',$res, $matches);

// Decoding `$matches[0]`
$obj = json_decode($matches[0]);

var_dump($obj);

You can just use the 4th line which is a json string. Demo

$arr = json_decode(explode(PHP_EOL,$res)[3],true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM