1

我想使用常量联系人的 API,并希望在用户注册到网站时使用 PHP 脚本插入用户电子邮件 ID。

我为此使用以下代码。

    $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."}]

请帮助我如何解决此错误。

4

1 回答 1

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"
}
于 2017-11-06T05:22:28.623 回答