Jamersan | Magento and BigCommerce Elite Partner Agency

Using Guzzle Without Composer

While working on a module to integrate Magento with Foxy Cart’s awesome checkout service, I came across the need to use Guzzle in my Magento module. Because I am building the initial version of this module using Magento 1.x, I could not be certain that users of this module would have Composer available.

To accomplish this task, I had to put together a basic autoloader.php class that would serve as my autoloader for not only Guzzle, but any library that it or my script requires.

To start, I create a Magento module like any other module. In my app/code/Namespace/Module folder, I create a folder named “lib” for my includes. In this folder, I added the following libraries:

1. Doctrine/Common/Cache
2. GuzzleHttp
3. React/Promise
4. Riimu/Kit

I also added a file named autoloader.php. I started with the proper autoloader.php from the zip file download of GuzzleHttp, but then added all the other library includes that I may need. The resulting file is below:


$mapping = array( 'React\Promise\CancellablePromiseInterface' => __DIR__ . '/React/Promise/CancellablePromiseInterface.php',<br ?--> 'React\Promise\Deferred' => __DIR__ . '/React/Promise/Deferred.php',
'React\Promise\ExtendedPromiseInterface' => __DIR__ . '/React/Promise/ExtendedPromiseInterface.php',
'React\Promise\FulfilledPromise' => __DIR__ . '/React/Promise/FulfilledPromise.php',
'React\Promise\LazyPromise' => __DIR__ . '/React/Promise/LazyPromise.php',
'React\Promise\Promise' => __DIR__ . '/React/Promise/Promise.php',
'React\Promise\PromiseInterface' => __DIR__ . '/React/Promise/PromiseInterface.php',
'React\Promise\PromisorInterface' => __DIR__ . '/React/Promise/PromisorInterface.php',
'React\Promise\RejectedPromise' => __DIR__ . '/React/Promise/RejectedPromise.php',
'React\Promise\UnhandledRejectionException' => __DIR__ . '/React/Promise/UnhandledRejectionException.php',
'React\Promise\functions' => __DIR__ . '/React/Promise/functions.php',
'React\Promise\functions_include' => __DIR__ . '/React/Promise/functions_include.php',
'GuzzleHttp\Stream\AppendStream' => __DIR__ . '/GuzzleHttp/Stream/AppendStream.php',
'GuzzleHttp\Stream\AsyncReadStream' => __DIR__ . '/GuzzleHttp/Stream/AsyncReadStream.php',
'GuzzleHttp\Stream\BufferStream' => __DIR__ . '/GuzzleHttp/Stream/BufferStream.php',
'GuzzleHttp\Stream\CachingStream' => __DIR__ . '/GuzzleHttp/Stream/CachingStream.php',
'GuzzleHttp\Stream\DroppingStream' => __DIR__ . '/GuzzleHttp/Stream/DroppingStream.php',
'GuzzleHttp\Stream\Exception\CannotAttachException' => __DIR__ . '/GuzzleHttp/Stream/Exception/CannotAttachException.php',
'GuzzleHttp\Stream\Exception\SeekException' => __DIR__ . '/GuzzleHttp/Stream/Exception/SeekException.php',
'GuzzleHttp\Stream\FnStream' => __DIR__ . '/GuzzleHttp/Stream/FnStream.php',
'GuzzleHttp\Stream\GuzzleStreamWrapper' => __DIR__ . '/GuzzleHttp/Stream/GuzzleStreamWrapper.php',
'GuzzleHttp\Stream\InflateStream' => __DIR__ . '/GuzzleHttp/Stream/InflateStream.php',
'GuzzleHttp\Stream\LazyOpenStream' => __DIR__ . '/GuzzleHttp/Stream/LazyOpenStream.php',
'GuzzleHttp\Stream\LimitStream' => __DIR__ . '/GuzzleHttp/Stream/LimitStream.php',
'GuzzleHttp\Stream\MetadataStreamInterface' => __DIR__ . '/GuzzleHttp/Stream/MetadataStreamInterface.php',
'GuzzleHttp\Stream\NoSeekStream' => __DIR__ . '/GuzzleHttp/Stream/NoSeekStream.php',
'GuzzleHttp\Stream\NullStream' => __DIR__ . '/GuzzleHttp/Stream/NullStream.php',
'GuzzleHttp\Stream\PumpStream' => __DIR__ . '/GuzzleHttp/Stream/PumpStream.php',
'GuzzleHttp\Stream\Stream' => __DIR__ . '/GuzzleHttp/Stream/Stream.php',
'GuzzleHttp\Stream\StreamDecoratorTrait' => __DIR__ . '/GuzzleHttp/Stream/StreamDecoratorTrait.php',
'GuzzleHttp\Stream\StreamInterface' => __DIR__ . '/GuzzleHttp/Stream/StreamInterface.php',
'GuzzleHttp\Stream\Utils' => __DIR__ . '/GuzzleHttp/Stream/Utils.php',
'GuzzleHttp\Ring\Client\ClientUtils' => __DIR__ . '/GuzzleHttp/Ring/Client/ClientUtils.php',
'GuzzleHttp\Ring\Client\CurlFactory' => __DIR__ . '/GuzzleHttp/Ring/Client/CurlFactory.php',
'GuzzleHttp\Ring\Client\CurlHandler' => __DIR__ . '/GuzzleHttp/Ring/Client/CurlHandler.php',
'GuzzleHttp\Ring\Client\CurlMultiHandler' => __DIR__ . '/GuzzleHttp/Ring/Client/CurlMultiHandler.php',
'GuzzleHttp\Ring\Client\Middleware' => __DIR__ . '/GuzzleHttp/Ring/Client/Middleware.php',
'GuzzleHttp\Ring\Client\MockHandler' => __DIR__ . '/GuzzleHttp/Ring/Client/MockHandler.php',
'GuzzleHttp\Ring\Client\StreamHandler' => __DIR__ . '/GuzzleHttp/Ring/Client/StreamHandler.php',
'GuzzleHttp\Ring\Core' => __DIR__ . '/GuzzleHttp/Ring/Core.php',
'GuzzleHttp\Ring\Exception\CancelledException' => __DIR__ . '/GuzzleHttp/Ring/Exception/CancelledException.php',
'GuzzleHttp\Ring\Exception\CancelledFutureAccessException' => __DIR__ . '/GuzzleHttp/Ring/Exception/CancelledFutureAccessException.php',
'GuzzleHttp\Ring\Exception\ConnectException' => __DIR__ . '/GuzzleHttp/Ring/Exception/ConnectException.php',
'GuzzleHttp\Ring\Exception\RingException' => __DIR__ . '/GuzzleHttp/Ring/Exception/RingException.php',
'GuzzleHttp\Ring\Future\BaseFutureTrait' => __DIR__ . '/GuzzleHttp/Ring/Future/BaseFutureTrait.php',
'GuzzleHttp\Ring\Future\CompletedFutureArray' => __DIR__ . '/GuzzleHttp/Ring/Future/CompletedFutureArray.php',
'GuzzleHttp\Ring\Future\CompletedFutureValue' => __DIR__ . '/GuzzleHttp/Ring/Future/CompletedFutureValue.php',
'GuzzleHttp\Ring\Future\FutureArray' => __DIR__ . '/GuzzleHttp/Ring/Future/FutureArray.php',
'GuzzleHttp\Ring\Future\FutureArrayInterface' => __DIR__ . '/GuzzleHttp/Ring/Future/FutureArrayInterface.php',
'GuzzleHttp\Ring\Future\FutureInterface' => __DIR__ . '/GuzzleHttp/Ring/Future/FutureInterface.php',
'GuzzleHttp\Ring\Future\FutureValue' => __DIR__ . '/GuzzleHttp/Ring/Future/FutureValue.php',
'GuzzleHttp\Ring\Future\MagicFutureTrait' => __DIR__ . '/GuzzleHttp/Ring/Future/MagicFutureTrait.php',
'GuzzleHttp\BatchResults' => __DIR__ . '/GuzzleHttp/BatchResults.php',
'GuzzleHttp\Client' => __DIR__ . '/GuzzleHttp/Client.php',
'GuzzleHttp\ClientInterface' => __DIR__ . '/GuzzleHttp/ClientInterface.php',
'GuzzleHttp\Collection' => __DIR__ . '/GuzzleHttp/Collection.php',
'GuzzleHttp\Cookie\CookieJar' => __DIR__ . '/GuzzleHttp/Cookie/CookieJar.php',
'GuzzleHttp\Cookie\CookieJarInterface' => __DIR__ . '/GuzzleHttp/Cookie/CookieJarInterface.php',
'GuzzleHttp\Cookie\FileCookieJar' => __DIR__ . '/GuzzleHttp/Cookie/FileCookieJar.php',
'GuzzleHttp\Cookie\SessionCookieJar' => __DIR__ . '/GuzzleHttp/Cookie/SessionCookieJar.php',
'GuzzleHttp\Cookie\SetCookie' => __DIR__ . '/GuzzleHttp/Cookie/SetCookie.php',
'GuzzleHttp\Event\AbstractEvent' => __DIR__ . '/GuzzleHttp/Event/AbstractEvent.php',
'GuzzleHttp\Event\AbstractRequestEvent' => __DIR__ . '/GuzzleHttp/Event/AbstractRequestEvent.php',
'GuzzleHttp\Event\AbstractRetryableEvent' => __DIR__ . '/GuzzleHttp/Event/AbstractRetryableEvent.php',
'GuzzleHttp\Event\AbstractTransferEvent' => __DIR__ . '/GuzzleHttp/Event/AbstractTransferEvent.php',
'GuzzleHttp\Event\BeforeEvent' => __DIR__ . '/GuzzleHttp/Event/BeforeEvent.php',
'GuzzleHttp\Event\CompleteEvent' => __DIR__ . '/GuzzleHttp/Event/CompleteEvent.php',
'GuzzleHttp\Event\Emitter' => __DIR__ . '/GuzzleHttp/Event/Emitter.php',
'GuzzleHttp\Event\EmitterInterface' => __DIR__ . '/GuzzleHttp/Event/EmitterInterface.php',
'GuzzleHttp\Event\EndEvent' => __DIR__ . '/GuzzleHttp/Event/EndEvent.php',
'GuzzleHttp\Event\ErrorEvent' => __DIR__ . '/GuzzleHttp/Event/ErrorEvent.php',
'GuzzleHttp\Event\EventInterface' => __DIR__ . '/GuzzleHttp/Event/EventInterface.php',
'GuzzleHttp\Event\HasEmitterInterface' => __DIR__ . '/GuzzleHttp/Event/HasEmitterInterface.php',
'GuzzleHttp\Event\HasEmitterTrait' => __DIR__ . '/GuzzleHttp/Event/HasEmitterTrait.php',
'GuzzleHttp\Event\ListenerAttacherTrait' => __DIR__ . '/GuzzleHttp/Event/ListenerAttacherTrait.php',
'GuzzleHttp\Event\ProgressEvent' => __DIR__ . '/GuzzleHttp/Event/ProgressEvent.php',
'GuzzleHttp\Event\RequestEvents' => __DIR__ . '/GuzzleHttp/Event/RequestEvents.php',
'GuzzleHttp\Event\SubscriberInterface' => __DIR__ . '/GuzzleHttp/Event/SubscriberInterface.php',
'GuzzleHttp\Exception\BadResponseException' => __DIR__ . '/GuzzleHttp/Exception/BadResponseException.php',
'GuzzleHttp\Exception\ClientException' => __DIR__ . '/GuzzleHttp/Exception/ClientException.php',
'GuzzleHttp\Exception\ConnectException' => __DIR__ . '/GuzzleHttp/Exception/ConnectException.php',
'GuzzleHttp\Exception\CouldNotRewindStreamException' => __DIR__ . '/GuzzleHttp/Exception/CouldNotRewindStreamException.php',
'GuzzleHttp\Exception\ParseException' => __DIR__ . '/GuzzleHttp/Exception/ParseException.php',
'GuzzleHttp\Exception\RequestException' => __DIR__ . '/GuzzleHttp/Exception/RequestException.php',
'GuzzleHttp\Exception\ServerException' => __DIR__ . '/GuzzleHttp/Exception/ServerException.php',
'GuzzleHttp\Exception\StateException' => __DIR__ . '/GuzzleHttp/Exception/StateException.php',
'GuzzleHttp\Exception\TooManyRedirectsException' => __DIR__ . '/GuzzleHttp/Exception/TooManyRedirectsException.php',
'GuzzleHttp\Exception\TransferException' => __DIR__ . '/GuzzleHttp/Exception/TransferException.php',
'GuzzleHttp\Exception\XmlParseException' => __DIR__ . '/GuzzleHttp/Exception/XmlParseException.php',
'GuzzleHttp\HasDataTrait' => __DIR__ . '/GuzzleHttp/HasDataTrait.php',
'GuzzleHttp\Message\AbstractMessage' => __DIR__ . '/GuzzleHttp/Message/AbstractMessage.php',
'GuzzleHttp\Message\AppliesHeadersInterface' => __DIR__ . '/GuzzleHttp/Message/AppliesHeadersInterface.php',
'GuzzleHttp\Message\FutureResponse' => __DIR__ . '/GuzzleHttp/Message/FutureResponse.php',
'GuzzleHttp\Message\MessageFactory' => __DIR__ . '/GuzzleHttp/Message/MessageFactory.php',
'GuzzleHttp\Message\MessageFactoryInterface' => __DIR__ . '/GuzzleHttp/Message/MessageFactoryInterface.php',
'GuzzleHttp\Message\MessageInterface' => __DIR__ . '/GuzzleHttp/Message/MessageInterface.php',
'GuzzleHttp\Message\MessageParser' => __DIR__ . '/GuzzleHttp/Message/MessageParser.php',
'GuzzleHttp\Message\Request' => __DIR__ . '/GuzzleHttp/Message/Request.php',
'GuzzleHttp\Message\RequestInterface' => __DIR__ . '/GuzzleHttp/Message/RequestInterface.php',
'GuzzleHttp\Message\Response' => __DIR__ . '/GuzzleHttp/Message/Response.php',
'GuzzleHttp\Message\ResponseInterface' => __DIR__ . '/GuzzleHttp/Message/ResponseInterface.php',
'GuzzleHttp\Mimetypes' => __DIR__ . '/GuzzleHttp/Mimetypes.php',
'GuzzleHttp\Pool' => __DIR__ . '/GuzzleHttp/Pool.php',
'GuzzleHttp\Post\MultipartBody' => __DIR__ . '/GuzzleHttp/Post/MultipartBody.php',
'GuzzleHttp\Post\PostBody' => __DIR__ . '/GuzzleHttp/Post/PostBody.php',
'GuzzleHttp\Post\PostBodyInterface' => __DIR__ . '/GuzzleHttp/Post/PostBodyInterface.php',
'GuzzleHttp\Post\PostFile' => __DIR__ . '/GuzzleHttp/Post/PostFile.php',
'GuzzleHttp\Post\PostFileInterface' => __DIR__ . '/GuzzleHttp/Post/PostFileInterface.php',
'GuzzleHttp\Query' => __DIR__ . '/GuzzleHttp/Query.php',
'GuzzleHttp\QueryParser' => __DIR__ . '/GuzzleHttp/QueryParser.php',
'GuzzleHttp\RequestFsm' => __DIR__ . '/GuzzleHttp/RequestFsm.php',
'GuzzleHttp\RingBridge' => __DIR__ . '/GuzzleHttp/RingBridge.php',
'GuzzleHttp\Subscriber\Cookie' => __DIR__ . '/GuzzleHttp/Subscriber/Cookie.php',
'GuzzleHttp\Subscriber\History' => __DIR__ . '/GuzzleHttp/Subscriber/History.php',
'GuzzleHttp\Subscriber\HttpError' => __DIR__ . '/GuzzleHttp/Subscriber/HttpError.php',
'GuzzleHttp\Subscriber\Mock' => __DIR__ . '/GuzzleHttp/Subscriber/Mock.php',
'GuzzleHttp\Subscriber\Prepare' => __DIR__ . '/GuzzleHttp/Subscriber/Prepare.php',
'GuzzleHttp\Subscriber\Redirect' => __DIR__ . '/GuzzleHttp/Subscriber/Redirect.php',
'GuzzleHttp\ToArrayInterface' => __DIR__ . '/GuzzleHttp/ToArrayInterface.php',
'GuzzleHttp\Transaction' => __DIR__ . '/GuzzleHttp/Transaction.php',
'GuzzleHttp\UriTemplate' => __DIR__ . '/GuzzleHttp/UriTemplate.php',
'GuzzleHttp\Url' => __DIR__ . '/GuzzleHttp/Url.php',
'GuzzleHttp\Utils' => __DIR__ . '/GuzzleHttp/Utils.php',
'GuzzleHttp\Subscriber\Cache\CacheSubscriber' => __DIR__ . '/GuzzleHttp/Subscriber/Cache/CacheSubscriber.php',
'GuzzleHttp\Subscriber\Cache\CacheStorage' => __DIR__ . '/GuzzleHttp/Subscriber/Cache/CacheStorage.php',
'GuzzleHttp\Subscriber\Cache\CacheStorageInterface' => __DIR__ . '/GuzzleHttp/Subscriber/Cache/CacheStorageInterface.php',
'GuzzleHttp\Subscriber\Cache\ValidationSubscriber' => __DIR__ .
'/GuzzleHttp/Subscriber/Cache/ValidationSubscriber.php',
'GuzzleHttp\Subscriber\Cache\PurgeSubscriber' => __DIR__ .
'/GuzzleHttp/Subscriber/Cache/PurgeSubscriber.php',
'GuzzleHttp\Subscriber\Cache\Utils' => __DIR__ .
'/GuzzleHttp/Subscriber/Cache/Utils.php',
'Doctrine\Common\Cache\ArrayCache' => __DIR__ . '/Doctrine/Common/Cache/ArrayCache.php',
'Doctrine\Common\Cache\CacheProvider' => __DIR__ . '/Doctrine/Common/Cache/CacheProvider.php',
'Doctrine\Common\Cache\Cache' => __DIR__ . '/Doctrine/Common/Cache/Cache.php',
'Doctrine\Common\Cache\FlushableCache' => __DIR__ . '/Doctrine/Common/Cache/FlushableCache.php',
'Doctrine\Common\Cache\ClearableCache' => __DIR__ . '/Doctrine/Common/Cache/ClearableCache.php',
'Doctrine\Common\Cache\MultiGetCache' => __DIR__ . '/Doctrine/Common/Cache/MultiGetCache.php',
'Doctrine\Common\Cache\MultiPutCache' => __DIR__ . '/Doctrine/Common/Cache/MultiPutCache.php',
'Riimu\Kit\CSRF\CSRFHandler' => __DIR__ . '/Riimu/Kit/CSRF/CSRFHandler.php',
'Riimu\Kit\CSRF\Storage\SessionStorage' => __DIR__ . '/Riimu/Kit/CSRF/Storage/SessionStorage.php',
'Riimu\Kit\CSRF\Storage\TokenStorage' => __DIR__ . '/Riimu/Kit/CSRF/Storage/TokenStorage.php',
'Riimu\Kit\CSRF\Source\PostSource' => __DIR__ . '/Riimu/Kit/CSRF/Source/PostSource.php',
'Riimu\Kit\CSRF\Source\TokenSource' => __DIR__ . '/Riimu/Kit/CSRF/Source/TokenSource.php',
'Riimu\Kit\CSRF\Source\HeaderSource' => __DIR__ . '/Riimu/Kit/CSRF/Source/HeaderSource.php',
'Riimu\Kit\SecureRandom\SecureRandom' => __DIR__ . '/Riimu/Kit/SecureRandom/SecureRandom.php',
'Riimu\Kit\SecureRandom\Generator\RandomReader' => __DIR__ . '/Riimu/Kit/SecureRandom/Generator/RandomReader.php',
'Riimu\Kit\SecureRandom\Generator\AbstractGenerator' => __DIR__ . '/Riimu/Kit/SecureRandom/Generator/AbstractGenerator.php',
'Riimu\Kit\SecureRandom\Generator\Generator' => __DIR__ . '/Riimu/Kit/SecureRandom/Generator/Generator.php'
);

spl_autoload_register(function ($class) use ($mapping) {
if (isset($mapping[$class])) {
require $mapping[$class];
}
}, true);

require __DIR__ . ‘/React/Promise/functions.php’;

 

In my class that needs to use Guzzle, I then add this to the top of the class file:


require Mage::getModuleDir('','Jamersan_Foxy').DS.'lib'.DS.'autoloader.php';
use GuzzleHttp\Subscriber\Cache\CacheSubscriber;
use GuzzleHttp\Client;

That should do it! After adding these items, you should now be able to use Guzzle in your Magento module!

Our Leadership

TJ Gamble

CEO

Shane Rodgers

Director, Operations

Alex Schreck

Director, Sales & Partnerships

Dave Christy BigCommerce & Adobe Expert

Dave Christy

Director, Delivery