To install, just issue the following PowerShell command at the root of OpenMage:
PS D:\Work\project> composer require guzzlehttp/guzzle:^7.0
Following is deprecated in the latest OpenMage, which will autoload the vendor folder automatically.
First, take a look at app\Mage.php autoload at lines 54-56:
/* Support additional includes, such as composer's vendor/autoload.php files */
foreach (glob(BP . DS . 'app' . DS . 'etc' . DS . 'includes' . DS . '*.php') as $path) {
    include_once $path;
}
So, we can add a file to load composer's autoload like this:
// app\etc\includes\composer.php
<?php
require BP . DS .'vendor/autoload.php';
I will install it in my local PC:
- [powershell] composer self-update --stableTo update PHP composer to ver 2.
- [browser] Download and upgrade exe files for WAMPServer, especially PHP 7.4.20
- [edit the system environment variables] Click button Environment Variables… > Edit Path, look for the path to old PHP and edit to new path, example: D:\Work\wamp64\bin\php\php7.4.20
- [powershell] composer require guzzlehttp/guzzle:^7.0Ref guzzle official site.
This will install Guzzle package in ./vendor/ along with a bunch of other files in different folders:
When it's ready for production, commit all and deploy.
Sample code to test if it works:
    public function guzzAction()
    {
        $msg = [];
        $apiKey = '';
        $username = '';
        $password = '';
        $body = [
            'key1' => 'value1',
            'key2' => 'value2',
            // ...
        ];
        $client = new GuzzleHttp\Client();
        try {
            $response = $client->request(
                'POST', 
                'https://domain.com/endpoint', 
                [
                    'auth' => [$username, $password],
                    'headers' => ['apiKey' => $apiKey],
                    'json' => $body,
                    //'debug' => true
                ]
            );
            $msg['statusCode'] = $response->getStatusCode();
            $msg['reasonPhrase'] = $response->getReasonPhrase();
            $msg['contentType'] = $response->getHeader('content-type')[0];
            $msg['bodyContent'] = $response->getBody()->getContents();
            if (strpos($msg['contentType'], 'application/json') === 0) {
                $msg['bodyContentDecode'] = json_decode($msg['bodyContent'], true);
            }
            print_r($msg);
            return;
        } catch (\GuzzleHttp\Exception\RequestException $e) {
            if ($e->hasResponse()) {
                $response = $e->getResponse();
                $msg['statusCode'] = $response->getStatusCode();
                $msg['reasonPhrase'] = $response->getReasonPhrase();
                $msg['contentType'] = $response->getHeader('content-type')[0];
                $msg['bodyContent'] = $response->getBody()->getContents();
                if (strpos($msg['contentType'], 'application/json') === 0) {
                    $msg['bodyContentDecode'] = json_decode($msg['bodyContent'], true);
                }
                
            } else {
                $response = $e->getHandlerContext();
                $msg['error'] = $response['error'] ?? 'unknown';
            }
        }
        print_r($msg);
    }
Posted by kiatng to OpenMage (2021-06-22 01:46)