【问题标题】:How to include Guzzle in a wordpress theme/plugin如何在 wordpress 主题/插件中包含 Guzzle
【发布时间】:2016-02-27 11:46:19
【问题描述】:

如何将 Guzzle 包含到 wordpress 主题或插件中。由于主题/插件将分发给非技术用户,因此我不能要求他们通过 Composer 这样做。

我已经在 libs/resources/ 和 functions.php 的主题文件夹中上传了 guzzle 包,并且我已经为 guzzle 类定义了这个自动加载器

spl_autoload_register( 'guzzle_autoloader' );
function guzzle_autoloader( $class_name ) {
  if ( false !== strpos( $class_name, 'GuzzleHttp' ) ) {
  $classes_dir = realpath( plugin_dir_path( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'libs' . DIRECTORY_SEPARATOR.'resources'. DIRECTORY_SEPARATOR .'guzzle-master'. DIRECTORY_SEPARATOR .'src'. DIRECTORY_SEPARATOR ;

  $replace="GuzzleHttp\\";
  $class_file = str_replace($replace,'', $class_name);

  print 'request '.$classes_dir . $class_file.'</br>';

  require_once $classes_dir . $class_file.'.php';

  }
 }

但是我仍然收到此错误“调用未定义函数 GuzzleHttp\choose_handler()”

【问题讨论】:

  • 你有想过这个吗?

标签: wordpress guzzle


【解决方案1】:

作曲家自动加载怎么样?

  1. 只需要 Guzzle packagist repo 和 composer

     composer require guzzlehttp/guzzle
    
  2. 安装

     composer install
    
  3. 在您的 Wordpress 插件中自动加载 Guzzle 类

     require 'vendor/autoload.php';
     //use GuzzleHttp\Client;
    

【讨论】:

  • 如果您打算将插件与 Guzzle 库一起分发,则应为其添加前缀/范围以避免与其他库或插件的命名空间冲突。我创建了这个插件来展示它是如何完成的。 github.com/PHP-Prefixer/…
猜你喜欢
  • 2017-06-23
  • 1970-01-01
  • 1970-01-01
  • 2014-02-10
  • 2016-09-20
  • 2011-09-07
  • 1970-01-01
  • 1970-01-01
  • 2012-08-05
相关资源
最近更新 更多