尝试在php

时间:2019-09-24 13:53:52

标签: php json curl

我是新人,尝试在php中学习cURL,但是当尝试将新创建的用户发布到我的测试环境中时,出现错误。

我尝试调试并寻找各种东西,但是找不到任何具体错误的东西。我也尝试使用Google搜索,但是输入的所有答案都没有道理

string(830) "{"status":400,"error":"There was a problem in the JSON you submitted [92677fc5f9526c8878a88c61e3ca5a27]: unexpected characters after the JSON document () at line 1, column 1 [parse.c:618] 
in '--------------------------7c5256387e2a9ed5\r\nContent-Disposition: 
attachment; name=\"name\"\r\n\r\njohn jeff\r\n-------------------------

-7c5256387e2a9ed5\r\nContent-Disposition: attachment; 
name=\"email\"\r\n\r\nhelloyes@hotmail.com\r\n-------------------------

-7c5256387e2a9ed5\r\nContent-Disposition: attachment; name=\"phone\"\r\n\r\n09009090\r\n-------------------------

-7c5256387e2a9ed5\r\nContent-Disposition: attachment; name=\"companies\"\r\n\r\nLandstede\r\n-------------------------

-7c5256387e2a9ed5\r\nContent-Disposition: attachment; name=\"user_id\"\r\n\r\n420\r\n--------------------------
7c5256387e2a9ed5--\r\n"}"
if (isset($_POST['submit'])) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.intercom.io/users");
    curl_setopt($ch, CURLOPT_POST, 1);
    if (isset($_POST['unsubscribed_from_emails'])) {
        $post = ['name' => $_POST['name'],
            'email' => $_POST['email'],
            'phone' => $_POST['phone'],
            'companies' => $_POST['companies'],
            'user_id' => $_POST['user_id'],
            'unsubscribed_from_emails' => $_POST['unsubscribed_from_emails']
        ];
    } else {
        $post = ['name' => $_POST['name'],
            'email' => $_POST['email'],
            'phone' => $_POST['phone'],
            'companies' => $_POST['companies'],
            'user_id' => $_POST['user_id']
        ];
    }
  //var_dump($post);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);  //Post Fields
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $headers = [
        'Authorization:Bearer <MYKEYYOUCANTSEE>',
        'Accept: application/json',
        'Content-type: application/json',
    ];

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $server_output = curl_exec($ch);

    curl_close($ch);

  //  var_dump($server_output);
}

我只希望将用户添加到环境中,但没有任何反应。只有当我var_dump找到问题时,我才收到该错误消息。

1 个答案:

答案 0 :(得分:0)

您需要json_encode() $ post数据。

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));  //Post Fields