doueta6642 2017-11-06 04:44 采纳率: 0%
浏览 33
已采纳

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

I want to use API of the constant contact and want to insert user Email Ids using PHP script while user register to the website.

I using following code for this.

    $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);

When I run this script then its showing following error.

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

Please help me that how can I resolve this error.

  • 写回答

1条回答 默认 最新

  • dongzhent208577 2017-11-06 05:22
    关注

    The actual problem is validation error,which is:-

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

    So what i did is changed:-

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

    To:-

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

    And used this CURL request and data saved successfully.

    <?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);
    

    Output i got is:-

    {
        "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"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!