Felipe Xavier
Felipe Xavier

Reputation: 33

How do I use the data returned by a cURL request?

I am passing an array for a php page to test but I can not get the data!

$data = array(
              "user_id" => $login->id,
              "discount_id" => $discount,
              "product_id" => $product_id->id,
              "date_payment" => $test->id
            );

$fields_string = http_build_query($data);

$url = "http://localhost/dev/felipe/request-checkout.php";

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

$result = json_decode(curl_exec($ch));
curl_close($ch);

page.php:

extract($_POST);

$user_id =  $_POST['user_id']; 
$discount_id =  $_POST['discount_id'];
$product_id =  $_POST['product_id'];
$data_payment =  $_POST['date_payment'];


echo $user_id . ' - ' . $discount_id . ' - ' . $product_id . ' - ' . $data_payment;

show null in $result in cURL. what I'm missing? how do I use the data and have a response which I did cURL?

Upvotes: 0

Views: 130

Answers (1)

Petr Hejda
Petr Hejda

Reputation: 43481

curl_exec($ch) returns string in this format: "1 - 2 - 3 - 4".

When you try to run json_decode() on this string, it returns null because this is not correct json format.


Instead of the echo $user_id . ' - ' . $discount_id . ' - ' . $product_id . ' - ' . $data_payment;, you can endode an array/object to json string:

echo json_encode($_POST);

Upvotes: 1

Related Questions