为什么在PHP的Stripe API上返回null?

时间:2019-02-27 18:47:39

标签: php api

我正在尝试var转储下面代码的$ response,但是它返回null,我不知道为什么...

$token = $_POST['stripeToken'];
$email = $_POST['email'];
$name = $_POST['name'];


if(filter_var($email, FILTER_VALIDATE_EMAIL) && !empty($name) && 
!empty($token)){ 
$ch = curl_init();

$data = [
    'source' => $token, 
    'description' => $name, 
    'email' => $email
];



curl_setopt_array($ch, [

    CURLOPT_URL => 'https://api.stripe.com/v1/customers', 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_USERPWD => 'sk_test_************************', 
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    CURLOPT_POSTFIELDS => http_build_query($data)

]);

$response = json_decode(curl_exec($ch));

curl_close($ch);

var_dump($response);

die();

} 

有关信息,$ _POST变量位于另一个文件中,我验证了其中是否填充了表单的信息。

希望有人可以提供帮助

谢谢!

1 个答案:

答案 0 :(得分:0)

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.stripe.com/v1/customers');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "source=" . $token . "&email=" . $cEmail);
curl_setopt($ch, CURLOPT_USERPWD, 'sk_test_xxxxxxxxxx' . ':' . '');

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
$obj = json_decode($result);
curl_close($ch);
$customer = $obj->id;

不客气:)