Search code examples
phpapicurlchargify

Cannot post data correctly to Chargify via PHP cURL


I guess I'm not sending the data required by the API correctly. Any ideas what I am doing wrong?

$ch = curl_init("https://test.chargify.com/customers.json");

$data = array(
  'first_name' => 'Test',
  'last_name' => 'User',
  'email' => '[email protected]'
);

$data = json_encode($data);

curl_setopt_array($ch, array(
  CURLOPT_USERPWD => "yyyyyyyyyyyyyyy:x", // assume this is correct
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $data,
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data)
  )
));

$output = curl_exec($ch);
curl_close($ch);

It returns

{
errors: [
        "First name: cannot be blank.",
        "Last name: cannot be blank.",
        "Email address: cannot be blank."
    ]
}

Here's the documentation for the API: http://docs.chargify.com/api-customers


Solution

  • Try this:

    $data = array( 'customer' => array(
      'first_name' => 'Test',
      'last_name' => 'User',
      'email' => '[email protected]'
    ));