Hello . I am using Google Natural Language with PHP .
those methods are working -
- analyzeSentiment,
- analyzeEntities
- analyzeSyntax
*classifyText
it is not working -
- moderateText
- classifyText
do I have to give any other role for this service ?
I am giving those roles -
- Auto Ml Editor
- Auto Ml Predictor
- Viewer
do I have to give any other role to use moderateText and classifyText?
I am sending my php function bellow -
public function moderateText(string $text): array
{
$accessToken = $this->getAccessToken();
if (!$accessToken) {
return [‘error’ => ‘Failed to retrieve access token.’];
}
$api_url = 'https://language.googleapis.com/v1/documents:moderateText';
// 2. Construct Request Body for Google Cloud Vision API
$request_body = json_encode([
'document' => [
'type' => 'PLAIN_TEXT',
'content' => $text
],
'encodingType' => 'UTF8'
]);
// 3. Create a Guzzle Client
$client = new Client();
// 4. Create a Guzzle Request
$headers = [
'Authorization' => 'Bearer ' . $accessToken,
'Content-Type' => 'application/json',
];
$request = new Request('POST', $api_url, $headers, $request_body);
// 5. Send the Request
try {
$response = $client->send($request);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
return ['error' => "Guzzle Error: " . $e->getMessage()];
}
// 6. Get the Response Body
$body = $response->getBody()->getContents();
// 7. Parse the Response
$responseData = json_decode($body, true);
if (isset($responseData['moderationCategories'])) {
foreach ($responseData['moderationCategories'] as $category) {
echo "Category: " . $category['name'] . " (Confidence: " . $category['confidence'] . ")\n";
}
} else {
echo "No harmful content detected.\n";
}
}