dshgnt2008 2017-04-14 20:43
浏览 30
已采纳

使用PHP和cURL构建动态JSON Dict

I was wondering how I could merge the variables at the top of my PHP file with the dict in my cURL POST method.

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$comment = $_POST['comment'];

curl_setopt_array($curl, array(
      CURLOPT_URL => "CUSTOM_URL",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => "{\"email_address\": \"thisperson@gmail.com\", \"status\": \"subscribed\", \"merge_fields\": {\"FNAME\": \"Bob\", \"LNAME\": \"Dylan\"}}",
      CURLOPT_HTTPHEADER => array(
        "authorization: Basic UN:PW",
        "cache-control: no-cache",
        "content-type: application/json",
      ),
    ));

So essentially, how do I put First Name, Last Name, Email, etc into "{\"email_address\": \"thisperson@gmail.com\", \"status\": \"subscribed\", \"merge_fields\": {\"FNAME\": \"Bob\", \"LNAME\": \"Dylan\"}}" (to replace thisperson@gmail.com, Bob, etc.)

  • 写回答

3条回答 默认 最新

  • dtdr84101 2017-04-14 20:59
    关注

    Use json_encode.

    $data = [
        'email_address' => $_POST['email'],
        'status' => 'subscribed',
        'merge_fields' => [
            'FNAME' => $_POST['first_name'],
            'LNAME' => $_POST['last_name'],
        ],
    ];
    
    ...
    CURLOPT_POSTFIELDS => json_encode($data),
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?