Answers.Body: <p>With the help of James Smith answer by suggesting Guzzle I was able to put the request together.
  I will post the working answer for future reference.</p>
  <pre><code>use GuzzleHttp\Client as GuzzleClient;
  
  /**
   * Use Microsoft Azure Content Moderator API
   */
   $client = new GuzzleClient([
      'base_uri' =&gt; 'https://uksouth.api.cognitive.microsoft.com/',
      'http_errors' =&gt; false,
      'timeout' =&gt; 10
  ]);
  
  $response = $client-&gt;request(
      'POST',
      'contentmoderator/moderate/v1.0/ProcessText/Screen',
      [
          'headers' =&gt; [
              'Content-Type' =&gt; 'text/plain',
              'Ocp-Apim-Subscription-Key' =&gt; '1234', // Add your key here
          ],
          'query' =&gt; [
              'autocorrect' =&gt; false,
              'PII' =&gt; true,
              'listId' =&gt; null,
              'classify' =&gt; true
          ],
          'body' =&gt; 'Your string to moderate goes here'
      ]
  );
  
  echo $response-&gt;getBody();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-06T12:09:11.090Z
Answers.LastActivityDate: 2020-08-06T12:09:11.090Z
Answers.UserDisplayName: zizther
Answers.Body: <p>Craft includes <a href="http://docs.guzzlephp.org/en/stable/" rel="nofollow noreferrer">Guzzle</a> which is very full-featured... I'd use that.</p>
  <p>Sorry I don't have time for converting your example but here's a simplified version of how to use it in a Craft plugin:</p>
  <pre><code>$client = new \GuzzleHttp\Client([
      'base_uri' =&gt; 'https://www.example.com/api',
      'http_errors' =&gt; false,
      'timeout' =&gt; 10
  ]);
  
  $data = ['some' =&gt; 'stuff'];
  $uri = 'post-a-thing'
  
  $response = $client-&gt;request(
      'POST',
      $uri, [
          'body' =&gt; json_encode($data),
          'headers' =&gt; [
              'User-Agent' =&gt; 'CraftCMS',
              'Content-type' =&gt; 'application/json'
          ]
      ]
  );
  </code></pre>
  <p>(I usually set <code>http_errors</code> to false so that I can handle http error responses in my own code and fail more gracefully than Guzzle).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-05T15:52:04.160Z
Answers.Text: Thanks. I will look into that more
Answers.UserDisplayName: zizther
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-05T15:49:56.543Z
Answers.LastActivityDate: 2020-08-05T15:49:56.543Z
Answers.UserDisplayName: James Smith
Body: <p>I have a custom module which is moderating content before entry save, I am looking to use a Microsoft Azure API to handle the moderation, in their documentation they use a HTTP library to make the request.</p>
  <p>Is there something which Yii or Craft already offers which I could utilise, if so, how would I perform the same function which is below.</p>
  <p>The example provided by MS Azure is below. <code>{body}</code> would be content which I pass in.</p>
  <pre><code>&lt;?php
  // This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
  require_once 'HTTP/Request2.php';
  
  $request = new Http_Request2('https://westus.api.cognitive.microsoft.com/contentmoderator/moderate/v1.0/ProcessText/Screen');
  $url = $request-&gt;getUrl();
  
  $headers = array(
      // Request headers
      'Content-Type' =&gt; 'text/plain',
      'Ocp-Apim-Subscription-Key' =&gt; '{subscription key}',
  );
  
  $request-&gt;setHeader($headers);
  
  $parameters = array(
      // Request parameters
      'autocorrect' =&gt; '{boolean}',
      'PII' =&gt; '{boolean}',
      'listId' =&gt; '{string}',
      'classify' =&gt; 'True',
      'language' =&gt; '{string}',
  );
  
  $url-&gt;setQueryVariables($parameters);
  
  $request-&gt;setMethod(HTTP_Request2::METHOD_POST);
  
  // Request body
  $request-&gt;setBody(&quot;{body}&quot;);
  
  try
  {
      $response = $request-&gt;send();
      echo $response-&gt;getBody();
  }
  catch (HttpException $ex)
  {
      echo $ex;
  }
  </code></pre>
  <p>Thanks</p>
  
Tags: <plugin-development><modules><request>
Title: What is the best way to handle HTTP requests within a plugin using Yii or Craft?
ThreadText: # What is the best way to handle HTTP requests within a plugin using Yii or Craft?
  
  (Asked by: username_0 on 2020-08-05)
  
  I have a custom module which is moderating content before entry save, I am looking to use a Microsoft Azure API to handle the moderation, in their documentation they use a HTTP library to make the request.
  
  Is there something which Yii or Craft already offers which I could utilise, if so, how would I perform the same function which is below.
  
  The example provided by MS Azure is below. `{body}` would be content which I pass in.
  
  ```
  <?php
  // This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
  require_once 'HTTP/Request2.php';
  
  $request = new Http_Request2('https://westus.api.cognitive.microsoft.com/contentmoderator/moderate/v1.0/ProcessText/Screen');
  $url = $request->getUrl();
  
  $headers = array(
      // Request headers
      'Content-Type' => 'text/plain',
      'Ocp-Apim-Subscription-Key' => '{subscription key}',
  );
  
  $request->setHeader($headers);
  
  $parameters = array(
      // Request parameters
      'autocorrect' => '{boolean}',
      'PII' => '{boolean}',
      'listId' => '{string}',
      'classify' => 'True',
      'language' => '{string}',
  );
  
  $url->setQueryVariables($parameters);
  
  $request->setMethod(HTTP_Request2::METHOD_POST);
  
  // Request body
  $request->setBody("{body}");
  
  try
  {
      $response = $request->send();
      echo $response->getBody();
  }
  catch (HttpException $ex)
  {
      echo $ex;
  }
  
  ```
  
  Thanks
  
  **Tags:** plugin-development, modules, request
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2020-08-06. Score: 0 | Accepted answer
  
  With the help of username_1 answer by suggesting Guzzle I was able to put the request together.
  I will post the working answer for future reference.
  
  ```
  use GuzzleHttp\Client as GuzzleClient;
  
  /**
   * Use Microsoft Azure Content Moderator API
   */
   $client = new GuzzleClient([
      'base_uri' => 'https://uksouth.api.cognitive.microsoft.com/',
      'http_errors' => false,
      'timeout' => 10
  ]);
  
  $response = $client->request(
      'POST',
      'contentmoderator/moderate/v1.0/ProcessText/Screen',
      [
          'headers' => [
              'Content-Type' => 'text/plain',
              'Ocp-Apim-Subscription-Key' => '1234', // Add your key here
          ],
          'query' => [
              'autocorrect' => false,
              'PII' => true,
              'listId' => null,
              'classify' => true
          ],
          'body' => 'Your string to moderate goes here'
      ]
  );
  
  echo $response->getBody();
  
  ```
  
  
  ---
  
  ### Answer by username_1 on 2020-08-05. Score: 2
  
  Craft includes [Guzzle](http://docs.guzzlephp.org/en/stable/) which is very full-featured... I'd use that.
  
  Sorry I don't have time for converting your example but here's a simplified version of how to use it in a Craft plugin:
  
  ```
  $client = new \GuzzleHttp\Client([
      'base_uri' => 'https://www.example.com/api',
      'http_errors' => false,
      'timeout' => 10
  ]);
  
  $data = ['some' => 'stuff'];
  $uri = 'post-a-thing'
  
  $response = $client->request(
      'POST',
      $uri, [
          'body' => json_encode($data),
          'headers' => [
              'User-Agent' => 'CraftCMS',
              'Content-type' => 'application/json'
          ]
      ]
  );
  
  ```
  
  (I usually set `http_errors` to false so that I can handle http error responses in my own code and fail more gracefully than Guzzle).
  
  > **Comments:**
  > **username_0**: Thanks. I will look into that more  
  
  ---
  
  