Crerem
Crerem

Reputation: 1359

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() "

Upvotes: 6

Views: 11647

Answers (1)

Yassine Qoraiche
Yassine Qoraiche

Reputation: 1158

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;
    

Upvotes: 12

Related Questions