Tl;博士。Composer
您必须在其余代码之前安装并运行并添加此行:
require __DIR__ . '/vendor/autoload.php';
Coinbase PHP API 用于Composer
处理其依赖项,因此必须遵循 Github 上详述的安装过程以避免头痛。
Composer
读取 Coinbase PHP API 的作者提供的配置文件,并自动创建一个目录结构,其中包含所需的所有依赖项,最重要的是,自动加载脚本。
PHP 曾经是 100% 独立的,已经内置了过多的函数和类,所以许多 PHP 编码器(例如我)在切换到更模块化的方法时遇到了一些问题,在某种程度上类似于 Python 风格的pip
命令或到PEAR
Perl 星系等等,当然有一些重要的区别。
因此,请务必遵循以下顺序:
1) 假设您在 Linux 上,安装了本地 Web 服务器,并且您网站的文档根目录是/var/www/newsite
.
2) 输入您的文档根目录,下载最新的 Coinbase PHP API 版本并解压缩/解压缩。我建议去发布,而不是克隆存储库。
$
$ cd /var/www/newsite
$
$ tar xzvf coinbase-php-2.5.0.ta.gz
3) 现在你需要下载Composer
. 转到https://getcomposer.org/的主页,然后单击下载。按照命令行安装部分中的说明进行操作。
为了方便起见,我在这里报告它们,但它们可能会发生变化,因此请务必查看Composer
主页。从您的文档根目录:
$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
$ php composer-setup.php
$ php -r "unlink('composer-setup.php');"
4)最后一步,运行Composer
并等待他完成工作:
$ php composer.phar install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 26 installs, 0 updates, 0 removals
- Installing guzzlehttp/promises (v1.3.1): Downloading (100%)
- Installing psr/http-message (1.0.1): Downloading (100%)
- Installing guzzlehttp/psr7 (1.4.2): Downloading (100%)
- Installing guzzlehttp/guzzle (6.2.3): Downloading (100%)
- Installing psr/log (1.0.2): Downloading (100%)
- Installing symfony/yaml (v3.2.8): Downloading (100%)
- Installing sebastian/version (1.0.6): Downloading (100%)
- Installing sebastian/global-state (1.1.1): Downloading (100%)
- Installing sebastian/recursion-context (1.0.5): Downloading (100%)
- Installing sebastian/exporter (1.2.2): Downloading (100%)
- Installing sebastian/environment (1.3.8): Downloading (100%)
- Installing sebastian/diff (1.4.2): Downloading (100%)
- Installing sebastian/comparator (1.2.4): Downloading (100%)
- Installing doctrine/instantiator (1.0.5): Downloading (100%)
- Installing phpunit/php-text-template (1.2.1): Downloading (100%)
- Installing phpunit/phpunit-mock-objects (2.3.8): Downloading (100%)
- Installing phpunit/php-timer (1.0.9): Downloading (100%)
- Installing phpunit/php-file-iterator (1.4.2): Downloading (100%)
- Installing phpunit/php-token-stream (1.4.11): Downloading (100%)
- Installing phpunit/php-code-coverage (2.2.4): Downloading (100%)
- Installing webmozart/assert (1.2.0): Downloading (100%)
- Installing phpdocumentor/reflection-common (1.0): Downloading (100%)
- Installing phpdocumentor/type-resolver (0.2.1): Downloading (100%)
- Installing phpdocumentor/reflection-docblock (3.1.1): Downloading (100%)
- Installing phpspec/prophecy (v1.7.0): Downloading (100%)
- Installing phpunit/phpunit (4.8.35): Downloading (100%)
symfony/yaml suggests installing symfony/console (For validating YAML files using the lint command)
sebastian/global-state suggests installing ext-uopz (*)
phpunit/phpunit-mock-objects suggests installing ext-soap (*)
phpunit/php-code-coverage suggests installing ext-xdebug (>=2.2.1)
phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)
Writing lock file
Generating autoload files
$
5)注意上面的最后一行。Github 的 Coinbase PHP API 自述文件中的示例有点误导,因为Composer
它很好,并且创建了一个名为的文件autoload.php
,必须使用该文件以正确的方式加载新库。
所以,这里是你的代码修改为使用它,从而加载所有需要的依赖项:
<?php
require __DIR__ . '/vendor/autoload.php';
$apiKey = 'topsecret';
$apiSecret = 'topkey';
$configuration = Configuration::apiKey($apiKey, $apiSecret);
$client = Client::create($configuration);
$account = $client->getPrimaryAccount();
该行:
require __DIR__ . '/vendor/autoload.php';
应该有所作为。没有它,脚本退出屏幕上没有错误,但在 php 日志文件中有许多错误,但此行为取决于服务器配置。
希望这可以帮助!