Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include Guzzle in a wordpress theme/plugin

How can i include Guzzle into a wordpress theme or plugin. Since the theme/plugins will be distributed to non tech users i cannot ask them to do that via Composer.

I have uploaded the guzzle pack in a theme folder in libs/resources/ and in functions.php and I have defined this autoloader for guzzle classes only

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';

  }
 }

However i'm still getting this error "call to undefined function GuzzleHttp\choose_handler() "

like image 940
Crerem Avatar asked Dec 19 '22 22:12

Crerem


1 Answers

What about composer autoload?

  1. Simply Require Guzzle packagist repo with composer

     composer require guzzlehttp/guzzle
    
  2. Install

     composer install
    
  3. Simply autoload Guzzle Classes in your Wordpress plugin

     require 'vendor/autoload.php';
     //use GuzzleHttp\Client;
    
like image 51
Yassine Qoraiche Avatar answered Jan 09 '23 00:01

Yassine Qoraiche