mcgo/heroku-web-api

在您的PHP应用程序中使用Heroku Web API

v1.0.0rc1 2016-03-09 13:09 UTC

This package is auto-updated.

Last update: 2024-08-29 03:26:17 UTC


README

此包可用于扩展 dyno 并获取当前 dyno 大小信息。如有更多需要使用 heroku API 的需求,请创建一个 pull request,将很快实现。

安装包

您可以通过 composer 安装此包,如下所示,之后可以像使用任何其他 composer 包一样使用它(有关基本用法,请参阅 https://composer.php.ac.cn/doc/01-basic-usage.md)。

composer require mcgo/heroku-web-api

基本用法信息

要使用此包,您需要知道您的 heroku 密钥和可机器读取的应用名称。这两个都可以从您的 heroku 账户页面下的安全性中获取。此包使用两种方法来设置与您的 heroku 应用通信的凭证。

通过环境变量设置凭证

请确保您已为应用和密钥创建了环境变量,名称分别为 HEROKU_KEYHEROKU_APP。这些将自动使用。如果它们找不到,将抛出异常。

手动设置凭证

如果您不想使用环境变量或需要与多个应用通信,您可以直接使用适当的 setApp()setKey() 方法设置正确的值。请查看下面的示例。

作为 Laravel ServiceProvider 的使用

您可以在 Laravel 应用程序内部将此包用作服务提供者。只需将一行添加到您的应用程序配置提供者数组 config/app.php 中,如下所示

  'providers' => [   
    // ...   
    // Heroku Scaling
    McGo\HerokuWebAPI\HerokuWebAPIServiceProvider::class,
    // ...
 ],

并添加到您的别名单中

 'aliases' => [
   // ...
   'Heroku'   => McGo\HerokuWebAPI\Facade\Heroku::class,
   // ...

之后,您可以在应用程序的任何地方使用它,例如 Heroku::scaleDyno('worker', 1) 或在 blade 模板中显示您的 dyno 的当前大小 Heroku::getDynoSize('web')

示例

try {
  // Instantiate a new HerokuWebAPI object
  $heroku = new HerokuWebAPI();

  // Get the current dyno size of web dynos
  $web_size = $heroku->getDynoSize('web');

  // Get the current siez of worker dynos
  $worker_size = $heroku->getDynoSize('worker');

  // Scale worker to 100
  $heroku->scaleDyno('worker', 100);
  
  // Control another app with the same key. The $heroku methods setApp, setKey and scaleDyno support method chaining, so you could do something like this.
  $heroku->setApp('my-other-app')->scaleDyno('web', 1)->scaleDyno('worker', 0);
  
  // Please note, that if you use credentials only by settings them with the methods, it is important to do it directly
  // after newing up the Object. Otherwise it would result in a HerokuCredentialsNotSetException. This is no correct
  // usage and will only work if environment variables are set.
  $wrongusage = new HerokuWebAPI();
  $wrongusage->scaleDyno('web', 0)->setApp('my-app');

} catch (HerokuCredentialsNotSetException $e) {
  // The credentials are not set. Either do so by using the environment
  // variables HEROKU_KEY and HEROKU_APP or storing them by using the
  // appropriate methods setApp() and setKey();
  
} catch (HerokuRequestFailedException $e) {
  // The request to Heroku was not completed with a 200 status.
    
}