<p>Craft uses an HTTP library called Guzzle, and it's built-in so you can import and use it from within your plugin. Here's an example:</p>

<pre><code>// imports
use GuzzleHttp;

// GET request

$client = new GuzzleHttp\Client();
$response = $client-&gt;request('GET', 'https://example.com', [
  'query' =&gt; ['exampleParam' =&gt; 'example']
]);
$responseBody = json_decode($response-&gt;getBody(), true);

// POST request
$client = new GuzzleHttp\Client();
$response = $client-&gt;request('POST', 'https://example.com/api', [
  'form_params' =&gt; [
    'example' =&gt; 'example',
  ]
]);
$responseBody = json_decode($response-&gt;getBody(), true);
</code></pre>

<p>To figure out how to properly format your Guzzle request, refer to the <a href="http://docs.guzzlephp.org/en/stable/" rel="noreferrer">Guzzle Docs</a>.</p>

CC BY-SA 4.0
2020-01-21T13:42:15.333Z
2020-01-21T13:42:15.333Z
Jake Dohm
<p>I'd like to be able to make HTTP requests from within my plugin to hit an endpoint and get a response. How can I do this?</p>

CC BY-SA 4.0
2020-01-21T19:20:41.970Z
Something tells me you already knew the answer to this! ;)
Brad Bell
<guzzle><xmlhttprequest>
How to make an HTTP request from a plugin?
# How to make an HTTP request from a plugin?

(Asked by: username_0 on 2020-01-21)

I'd like to be able to make HTTP requests from within my plugin to hit an endpoint and get a response. How can I do this?

**Tags:** guzzle, xmlhttprequest
> **Comments:**
> **username_1**: Something tells me you already knew the answer to this! ;)  

---

## 1 answers

### Answer by username_0 on 2020-01-21. Score: 9

Craft uses an HTTP library called Guzzle, and it's built-in so you can import and use it from within your plugin. Here's an example:
// imports
use GuzzleHttp;

// GET request

$client = new GuzzleHttp\Client();
$response = $client->request('GET', 'https://example.com', [
  'query' => ['exampleParam' => 'example']
]);
$responseBody = json_decode($response->getBody(), true);

// POST request
$client = new GuzzleHttp\Client();
$response = $client->request('POST', 'https://example.com/api', [
  'form_params' => [
    'example' => 'example',
  ]
]);
$responseBody = json_decode($response->getBody(), true);

To figure out how to properly format your Guzzle request, refer to the Guzzle Docs.


---

