Poniżej ciąg otrzymany z odpowiedzi webhooka z MyOprators, teraz potrzebujemy odpowiedniego json'a do odczytu danych webhooka.

Możesz sprawdzić poniższy skrypt php, aby zidentyfikować odpowiedź przechwytywania sieci

$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);
0
Chavda Hem 20 listopad 2019, 09:06

2 odpowiedzi

Proponuję użyć wyrażenia regularnego.

Myślę, że zarówno prędkość jest wyodrębniona, jak i prymitywność logiki będzie wygodniejsza dla twojego rozwiązania problemu.

Użyj tego kodu.

$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);
0
Aaron Yordanyan 20 listopad 2019, 09:16

Możesz po prostu użyć czwartej linii, która jest ciągiem json. Demo

$arr = json_decode(explode(PHP_EOL,$res)[3],true);
0
LF00 20 listopad 2019, 09:48