Search code examples
phpfunctionsoapglobal-variablespreg-split

Function Variable - API Response preg_split result and pass created variable to new function


Hoping you can help me here. I am at a bit of a loss :/

I have a function that passes data via API to a server. It sends a response. I need to split the string that comes in and take the final result and make that a variable that can be used by future functions.

This is the result from the server once I have successfully passed the data

CLIENTID=101410;CLIENTREF=MZABOX2382;CONTACTID=22975

This is shown by

echo($add_client_response->AddClientKYCResult);

From this I need to get the numbers only of the CLIENTID and strip everything else out.

I have this snippet of code so far that isn't working

function AddClientKYCResult ( $data, $pilot ) {
$add_client_response = preg_split('/;/', $result['Result']);
$clientId = preg_split('/=/', $spl[0][0]);
$clientRef = preg_split('/=/', $spl[1][0]);
$contactId = preg_split('/=/', $spl[2][0]);
return $data;
echo($clientId);
}

Can someone help on this one?

TIA


Solution

  • To take in cognizance that CLIENTID can be anywhere in the string.

    $apiResponse = $add_client_response->AddClientKYCResult;
    //$apiResponse = "CLIENTID=101410;CLIENTREF=MZABOX2382;CONTACTID=22975";
    
    $add_client_response = explode(";",$apiResponse);
    $result = [];
    foreach($add_client_response as $part) {
          $partArr = explode("=", $part);      
          $result[$partArr[0]] = $partArr[1];
    }
    
    $clientId = $result["CLIENTID"];
    $clientRef = $result["CLIENTREF"];
    $contactId = $result["CONTACTID"];
    echo("clientId = $clientId<br/>");
    echo("clientRef = $clientRef<br/>");
    echo("contactId = $contactId");