├── .gitignore ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── Fetch.php ├── icon.svg ├── twigextensions └── FetchTwigExtension.php └── variables └── FetchVariable.php /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Fetch Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). 6 | 7 | ## 2.0.0-beta.1 - 2023-06-08 8 | 9 | ### Added 10 | - Initial Craft 4 release 11 | 12 | ## 1.3.0 - 2019-11-16 13 | 14 | Transfer of ownership 👀 15 | 16 | ## 1.2.0 - 2018-08-02 17 | 18 | ### Added 19 | - You can now return the string response instead of parsed JSON 20 | 21 | ## 1.1.2 - 2018-04-16 22 | 23 | ### Changed 24 | - New icon 25 | - Set Craft CMS minimum requirement to `^3.0.0` 26 | 27 | ## 1.1.1 - 2018-02-26 28 | 29 | ### Changed 30 | - Set minimum requirement to `^3.0.0-RC11` 31 | 32 | ## 1.1.0 - 2018-02-24 33 | 34 | ### Added 35 | - `fetch()` twig extension 36 | 37 | ## 1.0.2 - 2018-02-08 38 | 39 | ### Changed 40 | - Updated GitHub links to reflect branch change 41 | 42 | ## 1.0.1 - 2018-02-08 43 | 44 | ### Changed 45 | - Renamed the composer package name to `craft-fetch` 46 | 47 | ## 1.0.0 - 2018-01-15 48 | 49 | ### Added 50 | - Initial release 51 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Jalen Davenport 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | icon 2 | 3 | # Fetch plugin for Craft CMS 3.x 4 | 5 | Utilise the Guzzle HTTP client from within your Craft templates. 6 | 7 | ## Requirements 8 | 9 | This plugin requires Craft CMS 3.0.0 or later. 10 | 11 | ## Installation 12 | 13 | To install the plugin, follow these instructions. 14 | 15 | 1. Open your terminal and go to your Craft project: 16 | 17 | cd /path/to/project 18 | 19 | 2. Then tell Composer to load the plugin: 20 | 21 | composer require jalendport/craft-fetch 22 | 23 | 3. In the Control Panel, go to Settings → Plugins and click the “Install” button for Fetch. 24 | 25 | ## Using Fetch 26 | 27 | This plugin is built to work with the standard [Guzzle request options](http://docs.guzzlephp.org/en/stable/request-options.html) 28 | 29 | ### Parameters 30 | 31 | | Parameter | Example value | 32 | | --------- | ------------- | 33 | | `client` | `{ base_uri : 'https://gtmetrix.com', timeout : 10 } ` | 34 | | `method` | `'POST'` | 35 | | `destination` | `'api/0.1/test'` | 36 | | `options` | `{ auth : ['username', 'password'] }` | 37 | 38 | ### Example Usage 39 | 40 | #### Request 41 | 42 | ```twig 43 | {% set client = { 44 | base_uri : 'https://gtmetrix.com', 45 | timeout : 10 46 | } %} 47 | 48 | {% set options = { 49 | auth : ['username', 'password'], 50 | form_params : { 51 | url : 'https://www.google.co.uk' 52 | } 53 | } %} 54 | 55 | {% set request = fetch(client, 'POST', 'api/0.1/test', options) %} 56 | ``` 57 | 58 | #### Response (successful) 59 | 60 | ```json 61 | { 62 | "statusCode":200, 63 | "reason":"OK", 64 | "body": { 65 | "credits_left":30, 66 | "test_id":"JDHFbrt7", 67 | "poll_state_url":"https:\/\/gtmetrix.com\/api\/0.1\/test\/JDHFbrt7" 68 | } 69 | } 70 | ``` 71 | 72 | You can fetch the string response by adding an additional parameter of `false` like so: 73 | 74 | ```twig 75 | {% set request = fetch(client, 'POST', 'api/0.1/test', options, false) %} 76 | ``` 77 | 78 | #### Response (error) 79 | 80 | ```json 81 | { 82 | "error":true, 83 | "reason":"Client error: `POST https:\/\/gtmetrix.com\/api\/0.1\/test` resulted in a `401 Authorization Required` response:\n{\u0022error\u0022:\u0022Invalid e-mail and\/or API key\u0022}\n\n" 84 | } 85 | ``` 86 | 87 | ## Fetch Roadmap 88 | 89 | Some things to do, and ideas for potential features: 90 | 91 | Brought to you by [Luke Youell](https://github.com/lukeyouell) 92 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jalendport/craft-fetch", 3 | "description": "Utilise the Guzzle HTTP client from within your Craft templates.", 4 | "type": "craft-plugin", 5 | "version": "2.0.0-beta.1", 6 | "keywords": [ 7 | "craft", 8 | "cms", 9 | "craftcms", 10 | "craft-plugin", 11 | "guzzle", 12 | "fetch" 13 | ], 14 | "support": { 15 | "docs": "https://github.com/jalendport/craft-fetch/blob/v1/README.md", 16 | "issues": "https://github.com/jalendport/craft-fetch/issues" 17 | }, 18 | "license": "MIT", 19 | "authors": [ 20 | { 21 | "name": "Jalen Davenport", 22 | "homepage": "https://jalendport.com" 23 | }, 24 | { 25 | "name": "Luke Youell", 26 | "homepage": "https://github.com/lukeyouell" 27 | } 28 | ], 29 | "require": { 30 | "craftcms/cms": "^4.0.0" 31 | }, 32 | "autoload": { 33 | "psr-4": { 34 | "jalendport\\fetch\\": "src/" 35 | } 36 | }, 37 | "extra": { 38 | "name": "Fetch", 39 | "handle": "fetch", 40 | "schemaVersion": "1.0.0", 41 | "hasCpSettings": false, 42 | "hasCpSection": false, 43 | "changelogUrl": "https://raw.githubusercontent.com/jalendport/craft-fetch/v1/CHANGELOG.md", 44 | "class": "jalendport\\fetch\\Fetch" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Fetch.php: -------------------------------------------------------------------------------- 1 | view->registerTwigExtension(new FetchTwigExtension()); 42 | 43 | Event::on( 44 | CraftVariable::class, 45 | CraftVariable::EVENT_INIT, 46 | function (Event $event) { 47 | /** @var CraftVariable $variable */ 48 | $variable = $event->sender; 49 | $variable->set('fetch', FetchVariable::class); 50 | } 51 | ); 52 | 53 | Craft::info( 54 | Craft::t( 55 | 'fetch', 56 | '{name} plugin loaded', 57 | ['name' => $this->name] 58 | ), 59 | __METHOD__ 60 | ); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 8 | 9 | fetch-icon 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/twigextensions/FetchTwigExtension.php: -------------------------------------------------------------------------------- 1 | request($method, $destination, $request); 44 | 45 | if ($parseJson) { 46 | $body = json_decode($response->getBody(), true); 47 | } else { 48 | $body = (string)$response->getBody(); 49 | } 50 | 51 | return [ 52 | 'statusCode' => $response->getStatusCode(), 53 | 'reason' => $response->getReasonPhrase(), 54 | 'body' => $body 55 | ]; 56 | 57 | } catch (GuzzleException $e) { 58 | 59 | return [ 60 | 'error' => true, 61 | 'reason' => $e->getMessage() 62 | ]; 63 | 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/variables/FetchVariable.php: -------------------------------------------------------------------------------- 1 | request($method, $destination, $request); 44 | 45 | return [ 46 | 'statusCode' => $response->getStatusCode(), 47 | 'reason' => $response->getReasonPhrase(), 48 | 'body' => json_decode($response->getBody(), true) 49 | ]; 50 | 51 | } catch (Exception $e) { 52 | 53 | return [ 54 | 'error' => true, 55 | 'reason' => $e->getMessage() 56 | ]; 57 | 58 | } 59 | 60 | } 61 | 62 | } 63 | --------------------------------------------------------------------------------