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

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题