1

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

2

Craft includes Guzzle 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).

  • Thanks. I will look into that more – zizther Aug 5 '20 at 15:52
0

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.

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.