条纹 - PHP 致命错误:未找到“Stripe\Charge”类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28846062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:05:29  来源:igfitidea点击:

Stripe - PHP Fatal error: Class 'Stripe\Charge' not found

phpclass

提问by Coyney

I've been following the Stripe documentation and I am unable to create a "charge".

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

Charge.php

收费.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 [email protected]"
));
?>

I'm able to process the first command "\Stripe\Stripe::setApiKey("KEY_HERE");" but receive an error when processing the next and receive the following error: "Class 'Stripe\Charge' not found in /var/www/charge.php"

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

回答by CWill

Here is an updated answer to this question.

这是此问题的更新答案。

From Dana at Stripe:

来自 Stripe 的 Dana:

If you prefer not to use Composer, our latest PHP bindings (>=2.x) include a init.php file that you can add to your project. Download and unzip the folder whereever you'd like, then include this init.php at the top of your scripts you use to communicate with the Stripe API, changing the path to the location of this file. Just like this: require_once('/path/to/stripe-php/init.php')

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

And that's what worked for me.

这就是对我有用的东西。

回答by Matty B

If you don't use composer to install the Stripe libraryyou will need to manually include all of the Stripe classes.

如果您不使用composer 安装 Stripe 库,您将需要手动包含所有 Stripe 类。

Composer is the preferred way as it will handle the autoloading of classes. Here is a sample composer file:

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

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

And then from a command line you would need to run composer updatewhile in the directory for your project. Afterwards, just add require 'vendor/autoload.php';to the top of your php file.

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

Otherwise, replace require('/var/www/stripe-php-2.1.1/lib/Stripe.php');with this code to include all of the classes:

否则,替换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';
}