PHP-尝试POST到后端并返回500错误?

时间:2019-01-07 22:21:43

标签: php stripe-payments

我试图将信息发布到我的PHP后端以创建Stripe客户,但是由于某些原因,当我访问.php路径时,下面的代码返回500错误。知道为什么吗?下面的大多数内容都直接来自Stripe示例,所以我不确定为什么会发生...(为我缺乏PHP知识而道歉)。

编辑:

<?php // Create a customer using a Stripe token

// If you're using Composer, use Composer's autoload:
require_once('vendor/autoload.php');

ini_set('display_errors',1);
error_reporting(E_ALL);

// Be sure to replace this with your actual test API key
// (switch to the live key later)
\Stripe\Stripe::setApiKey("MYLIVEKEY");

if (!isset($_POST['api_version']))
{
    exit(http_response_code(400));
}

// Create Stripe Customer
try {

    $key = \Stripe\EphemeralKey::create(array(
    "customer" => $customerId, 
    "stripe_version" => $_POST['api_version']
        )
    );

   header('Content-Type: application/json');
    exit(json_encode($key));
} catch (Exception $e) {
    exit(http_response_code(500));
}

?>

1 个答案:

答案 0 :(得分:0)

阅读您的评论后,我发现您遇到以下错误:

  

调用未定义的函数http_response_code()

http_response_code()是PHP 5.4中引入的,因此我假设您运行的PHP版本低于5.4?

您可以升级PHP版本,也可以删除http_response_code()并设置自己的响应。

示例:

<?php

require_once('vendor/autoload.php');
ini_set('display_errors',1);
error_reporting(E_ALL);

\Stripe\Stripe::setApiKey("MYLIVEKEY");

if (!isset($_POST['api_version'])) 
{
    // Manually set your own header:
    header('HTTP/1.1 400 Bad Request');
    // Do other things here
}

try {

    $key = \Stripe\EphemeralKey::create(array(
        "customer" => $customerId, 
        "stripe_version" => $_POST['api_version']
    ));

   header('Content-Type: application/json');
   exit(json_encode($key));

} catch (Exception $e) {
   // Manually set your own header instead...
   header('HTTP/1.1 500 Internal Server Error');
   // Do other things here
}

?>