1

常時連絡のAPIを使いたいのですが、Webサイトへのユーザー登録時に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