ds78662302 2016-08-13 12:02
浏览 210
已采纳

如何使用cURL请求返回的数据?

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?

  • 写回答

1条回答 默认 最新

  • drxpt06820 2016-08-13 12:12
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。