tags:

views:

28

answers:

2

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.

+2  A: 

Use parse_str():

parse_str($txResult, $txArr);
var_dump($txArr);
Ignacio Vazquez-Abrams
Yeah that did the trick. Thanks very much.
Daelan
@Daelan: Will you be accepting this answer then?
Ignacio Vazquez-Abrams
A: 

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.

Paul