Daelan
Daelan

Reputation: 723

How do I Parse the response I get back from CURL?

I'm sending some data to an external URL using Curl. The server sends me back a response in a string like this:

trnApproved=0&trnId=10000002&messageId=7&messageText=DECLINE

I can assign this string to a variable like this:

$txResult = curl_exec( $ch );
echo "Result:<BR>"; echo $txResult;

But how do I use the data that is sent back? I need a way to get the value of each variable sent back so that I can use it in my PHP script.

Any help would be much appreciated.

Thanks.

Upvotes: 1

Views: 11183

Answers (2)

Paul Degnan
Paul Degnan

Reputation: 2002

The default behavior of Curl is to just dump the data you get back out to the browser. In order to instead capture it to a variable, you need:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$txResult = curl_exec($ch);

That default behavior has always annoyed me. Returning the data from the curl_exec() call seems by far the more correct choice to me.

Upvotes: 7

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798526

Use parse_str():

parse_str($txResult, $txArr);
var_dump($txArr);

Upvotes: 4

Related Questions