Php 经常联系:如何解决错误“;JSON有效负载不能为空。”;

Php 经常联系:如何解决错误“;JSON有效负载不能为空。”;,php,curl,constantcontact,Php,Curl,Constantcontact,我想使用API的不断联系,并希望插入用户电子邮件ID使用PHP脚本,而用户注册到网站 我正在为此使用以下代码 $data['addresses'][0]['address_type'] = 'BUSINESS'; $data['addresses'][0]['city'] = 'Belleville'; $data['addresses'][0]['country_code'] = 'CA'; $data['addresses'][0]['line1'] = '4

我想使用API的不断联系,并希望插入用户电子邮件ID使用PHP脚本,而用户注册到网站

我正在为此使用以下代码

    $data['addresses'][0]['address_type'] = 'BUSINESS';
    $data['addresses'][0]['city'] = 'Belleville';
    $data['addresses'][0]['country_code'] = 'CA';
    $data['addresses'][0]['line1'] = '47 Shawmut Ave.';
    $data['addresses'][0]['line2'] = 'Suite 404';
    $data['addresses'][0]['postal_code'] = '"K8b 5W6';
    $data['addresses'][0]['state_code'] = 'ON';

    $data['lists'][0]['id'] = "1554397204";

    $data['cell_phone'] = "555-555-5555";
    $data['company_name'] = "System Optimzations";
    $data['confirmed'] = false;

    $data['email_addresses'][0]['email_address'] = "username112@example.com";

    $data['fax'] = "555-555-5555";
    $data['first_name'] = "Manvendra";
    $data['home_phone'] = "555-555-5555";
    $data['job_title'] = "Systems Analyst 3";
    $data['last_name'] = "Rajpurohit";
    $data['prefix_name'] = "Mr.Martone";
    $data['work_phone'] = "555-555-5555";
$jsonstring = json_encode($data);
    // echo '<pre>'; print_r($data);

$posturl = "https://api.constantcontact.com/v2/contacts?action_by=ACTION_BY_OWNER&api_key=*******";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $posturl);
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
// curl_setopt($ch, CURLOPT_USERPWD, $authstr);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonstring);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json",'Authorization: Bearer *****
','Content-Length: ' . strlen($jsonstring)));
curl_setopt($ch, CURLOPT_HEADER, false); // Do not return headers
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); // If you set this to 0, it will take you to a page with the http response

$response = curl_exec($ch);
if (FALSE === $response)
throw new Exception(curl_error($ch), curl_errno($ch));
echo '<pre>'; print_r($response); die;
curl_close($ch);

请帮助我解决此错误。

实际问题是验证错误,即:-

$data['prefix_name'] = "tone"; // length need to be 4 or less only
所以我所做的改变了:-

<?php

  $data['addresses'][0]['address_type'] = 'BUSINESS';
    $data['addresses'][0]['city'] = 'Belleville';
    $data['addresses'][0]['country_code'] = 'CA';
    $data['addresses'][0]['line1'] = '47 Shawmut Ave.';
    $data['addresses'][0]['line2'] = 'Suite 404';
    $data['addresses'][0]['postal_code'] = '"K8b 5W6';
    $data['addresses'][0]['state_code'] = 'ON';

    $data['lists'][0]['id'] = "1554397204";

    $data['cell_phone'] = "555-555-5555";
    $data['company_name'] = "System Optimzations";
    $data['confirmed'] = false;

    $data['email_addresses'][0]['email_address'] = "username112@example.com";

    $data['fax'] = "555-555-5555";
    $data['first_name'] = "Manvendra";
    $data['home_phone'] = "555-555-5555";
    $data['job_title'] = "Systems Analyst 3";
    $data['last_name'] = "Rajpurohit";
    $data['prefix_name'] = "tone";
    $data['work_phone'] = "555-555-5555";
$jsonstring = json_encode($data);
//echo '<pre>'; print_r($data);

$posturl = "https://api.constantcontact.com/v2/contacts?action_by=ACTION_BY_OWNER&api_key=*******";
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonstring);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json",'Authorization: Bearer ******')); //check change here
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// this need to be 1

$response = curl_exec($ch);
if (FALSE === $response)
throw new Exception(curl_error($ch), curl_errno($ch));
echo '<pre>'; print_r($response); die;
curl_close($ch);
致:-

并使用此CURL请求并成功保存数据


不要在你的问题中提供你的秘密信息。这将给您带来未来的问题;这个错误仍然显示[{“error_key”:“json.payload.empty”,“error_message”:“json payload不能为空。”}]@manvendrajpurohit使用我的完整代码,包括Curl代码。如果添加了一次联系人,则不会再次添加。这就是为什么你会犯错误。尝试使用一些新数据并使用我的完整代码谢谢先生现在对我有效,如果有任何错误,我会让你知道。@manvendrajpurohit很高兴帮助你:):)你好先生,你在吗?
$data['prefix_name'] = "Mr.Martone";
$data['prefix_name'] = "tone"; // length need to be 4 or less only
<?php

  $data['addresses'][0]['address_type'] = 'BUSINESS';
    $data['addresses'][0]['city'] = 'Belleville';
    $data['addresses'][0]['country_code'] = 'CA';
    $data['addresses'][0]['line1'] = '47 Shawmut Ave.';
    $data['addresses'][0]['line2'] = 'Suite 404';
    $data['addresses'][0]['postal_code'] = '"K8b 5W6';
    $data['addresses'][0]['state_code'] = 'ON';

    $data['lists'][0]['id'] = "1554397204";

    $data['cell_phone'] = "555-555-5555";
    $data['company_name'] = "System Optimzations";
    $data['confirmed'] = false;

    $data['email_addresses'][0]['email_address'] = "username112@example.com";

    $data['fax'] = "555-555-5555";
    $data['first_name'] = "Manvendra";
    $data['home_phone'] = "555-555-5555";
    $data['job_title'] = "Systems Analyst 3";
    $data['last_name'] = "Rajpurohit";
    $data['prefix_name'] = "tone";
    $data['work_phone'] = "555-555-5555";
$jsonstring = json_encode($data);
//echo '<pre>'; print_r($data);

$posturl = "https://api.constantcontact.com/v2/contacts?action_by=ACTION_BY_OWNER&api_key=*******";
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonstring);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json",'Authorization: Bearer ******')); //check change here
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// this need to be 1

$response = curl_exec($ch);
if (FALSE === $response)
throw new Exception(curl_error($ch), curl_errno($ch));
echo '<pre>'; print_r($response); die;
curl_close($ch);
{
    "id": "1519826644",
    "status": "ACTIVE",
    "fax": "555-555-5555",
    "addresses": [{
        "id": "9ac8fd40-c2b2-11e7-aa57-d4ae5284344f",
        "line1": "47 Shawmut Ave.",
        "line2": "Suite 404",
        "line3": "",
        "city": "Belleville",
        "address_type": "BUSINESS",
        "state_code": "ON",
        "state": "Ontario",
        "country_code": "ca",
        "postal_code": "\"K8b",
        "sub_postal_code": "5W6"
    }],
    "notes": [],
    "confirmed": false,
    "lists": [{
        "id": "1554397204",
        "status": "ACTIVE"
    }],
    "source": "API",
    "email_addresses": [{
        "id": "9a914b70-c2b2-11e7-aa57-d4ae5284344f",
        "status": "ACTIVE",
        "confirm_status": "NO_CONFIRMATION_REQUIRED",
        "opt_in_source": "ACTION_BY_OWNER",
        "opt_in_date": "2017-11-06T05:23:21.000Z",
        "email_address": "username23333@example.com"
    }],
    "prefix_name": "tone",
    "first_name": "Manvendra",
    "middle_name": "",
    "last_name": "Rajpurohit",
    "job_title": "Systems Analyst 3",
    "company_name": "System Optimzations",
    "home_phone": "555-555-5555",
    "work_phone": "555-555-5555",
    "cell_phone": "555-555-5555",
    "custom_fields": [],
    "created_date": "2017-11-06T05:23:20.000Z",
    "modified_date": "2017-11-06T05:23:20.000Z",
    "source_details": "Web Devloping"
}