Sentiment text with PHP and Azure
$subscription_key = "XXXXX";
$endpoint = "https://XXXXX.cognitiveservices.azure.com";
$path = '/text/analytics/v2.1/sentiment';
function GetSentiment ($host, $path, $key, $data) {
// Make sure all text is UTF-8 encoded.
foreach ($data as &$item) {
foreach ($item as $ignore => &$value) {
$value['text'] = utf8_encode($value['text']);
}
}
$data = json_encode ($data);
$headers = "Content-type: text/json\r\n" .
"Content-Length: " . strlen($data) . "\r\n" .
"Ocp-Apim-Subscription-Key: $key\r\n";
// NOTE: Use the key 'http' even if you are making an HTTPS request. See:
// https://php.net/manual/en/function.stream-context-create.php
$options = array (
'http' => array (
'header' => $headers,
'method' => 'POST',
'content' => $data
)
);
$context = stream_context_create ($options);
$result = file_get_contents ($host . $path, false, $context);
return $result;
}
$result = GetSentiment($endpoint, $path, $subscription_key, $data_tosentiment);
print_r($result);
Comentarios
Publicar un comentario