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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度