【问题标题】:Stripe - PHP Fatal error: Class 'Stripe\Charge' not foundStripe - PHP 致命错误:找不到类“Stripe\Charge”
【发布时间】:2015-08-08 06:35:57
【问题描述】:

我一直在关注 Stripe 文档,但无法创建“收费”。

收费.php

require('/var/www/stripe-php-2.1.1/lib/Stripe.php');
\Stripe\Stripe::setApiKey("KEY_HERE");

\Stripe\Charge::create(array(
  "amount" => 400,
  "currency" => "usd",
  "source" => "TOKEN_HERE", // obtained with Stripe.js
  "description" => "Charge for test@example.com"
));
?>

我能够处理第一个命令“\Stripe\Stripe::setApiKey("KEY_HERE");”但在处理下一个时收到错误并收到以下错误: “在 /var/www/charge.php 中找不到类 'Stripe\Charge'”

【问题讨论】:

  • 将您的代码发布到 Stripe.php
  • 也许可以尝试使用 composer 安装 Stripe 或从 GitHub 重新获取。它已加载 \Stripe 但未加载 \Charge。

标签: php class


【解决方案1】:

这是此问题的更新答案。

来自 Stripe 的 Dana:

如果您不想使用 Composer,我们最新的 PHP 绑定 (>=2.x) 包括一个 init.php 文件,您可以将其添加到您的项目中。下载和 将文件夹解压缩到您想要的任何位置,然后将这个 init.php 包含在 用于与 Stripe API 通信的脚本的顶部, 更改此文件位置的路径。像这样: require_once('/path/to/stripe-php/init.php')

这对我有用。

【讨论】:

  • 这对我来说比选择的答案好得多。这应该是这里选择的答案。
  • 一种包含 Stripe 的简洁方式,无需其他依赖项。
  • 谢谢。这个答案结束了几天试图在不使用作曲家的情况下解决这个问题(我觉得很困惑)。
  • 这通常有效,但我使用 Stripe 获得的 init.php 文件不包含几个文件:DefaultLogger.php 和 LoggerInterface.php。 LoggerInterface 需要放在第一位。
【解决方案2】:

如果您不使用 composer to install the Stripe library,则需要手动包含所有 Stripe 类。

Composer 是首选方式,因为它将处理类的自动加载。这是一个示例作曲家文件:

{
  "require": {
    "stripe/stripe-php": "2.*"
  }
}

然后,您需要在项目目录中运行composer update。之后,只需将require 'vendor/autoload.php'; 添加到您的 php 文件的顶部即可。

否则,用此代码替换require('/var/www/stripe-php-2.1.1/lib/Stripe.php');以包含所有类:

$stripeClassesDir = __DIR__ . '/stripe-php-2.1.1/lib/';
$stripeUtilDir    = $stripeClassesDir . 'Util/';
$stripeErrorDir   = $stripeClassesDir . 'Error/';

set_include_path($stripeClassesDir . PATH_SEPARATOR . $stripeUtilDir . PATH_SEPARATOR . $stripeErrorDir);

function __autoload($class)
{
    $parts = explode('\\', $class);
    require end($parts) . '.php';
}

【讨论】:

  • 您还需要包含此文件夹: $stripeHttpClientDir = $stripeClassesDir 。 'HttpClient/';
猜你喜欢
  • 1970-01-01
  • 2015-05-27
  • 2019-05-14
  • 2020-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-12
  • 2017-01-18
相关资源
最近更新 更多