0

I am getting the response from curl as follows in a string:

Accepted=AVSAUTH:TEST:::829649376:N::U
ENTRYMETHOD=KEYED
historyid=829649376
MERCHANTORDERNUMBER=14700000186
orderid=646526156
PAYTYPE=MasterCard
recurid=0
refcode=829649376-TEST
result=1
Status=Accepted
transid=0

I want to get individual variables like status= Accepted so that i can able to store those in my database. Any help will be highly appreciable.

3 Answers 3

2

preg_match is your friend:

<?
$result = "Accepted=AVSAUTH:TEST:::829649376:N::U
ENTRYMETHOD=KEYED
historyid=829649376
MERCHANTORDERNUMBER=14700000186
orderid=646526156
PAYTYPE=MasterCard
recurid=0
refcode=829649376-TEST
result=1
Status=Accepted
transid=0";

preg_match_all("/^([^=]+)=(.*)$/m", $result, $regs, PREG_SET_ORDER);

$data = [];
foreach($regs as $reg) {
    $data[$reg[1]] = $reg[2];
}

print_r($data);
?>

$data will be an hash Array:

Array
(
    [Accepted] => AVSAUTH:TEST:::829649376:N::U
    [ENTRYMETHOD] => KEYED
    [historyid] => 829649376
    [MERCHANTORDERNUMBER] => 14700000186
    [orderid] => 646526156
    [PAYTYPE] => MasterCard
    [recurid] => 0
    [refcode] => 829649376-TEST
    [result] => 1
    [Status] => Accepted
    [transid] => 0
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Mats, u rock
1

The easiest way might be to loop over each line, and build it into a simple key/value array:

$results = [];

foreach (explode("\n", $string) as $line) {
  list ($key, $value) = explode('=', $line, 2);
  $results[$key] = $value;
}

echo $results['Status']; // Accepted

Comments

1

You can extract content of curl response as follows,

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
// ... 

$response = curl_exec($ch); // Curl response

// After your curl response
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close($ch);

var_dump($body);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.