无法通过PHP cURL将数据正确发布到Chargify

时间:2012-05-22 05:14:47

标签: php api curl chargify

我想我没有正确发送API所需的数据。我有什么想法吗?

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

$data = array(
  'first_name' => 'Test',
  'last_name' => 'User',
  'email' => 'user@test.com'
);

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

返回

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

以下是API的文档: http://docs.chargify.com/api-customers

1 个答案:

答案 0 :(得分:1)

试试这个:

$data = array( 'customer' => array(
  'first_name' => 'Test',
  'last_name' => 'User',
  'email' => 'user@test.com'
));