Постоянный контакт: как устранить ошибку «Полезная нагрузка JSON не может быть пустой.»

Я хочу использовать API постоянного контакта и хочу вставлять идентификаторы электронной почты пользователя с помощью сценария 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);

Когда я запускаю этот скрипт, он показывает следующую ошибку.

[{"error_key":"json.payload.empty","error_message":"JSON payload cannot be empty."}]

Пожалуйста, помогите мне, как я могу решить эту ошибку.

1

Решение

Фактическая проблема — ошибка проверки, которая:

[{"error_key":"json.max.length.violation","error_message":"#/prefix_name: Value exceeds maximum length of 4."}]

Так что я сделал, изменилось:

$data['prefix_name'] = "Mr.Martone";

Для того, чтобы: —

$data['prefix_name'] = "tone"; // length need to be 4 or less only

И использовал этот запрос CURL и данные успешно сохранены.

<?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"}
1

Другие решения

Других решений пока нет …