认知服务 // AI 视觉标签超过 PHP

Cognitive Services // AI Vision Tag over PHP

我的 PHP 标记某些图像的请求不起作用,我得到一个错误。

{"error":{"code":"401","message":"Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource."}}

这是我的 PHP 带有 Curl 的脚本。

$ocpApimSubscriptionKey = 'MYKEY 1 FROM ccounts/cskeys';
$uriBase = 'https://MY ENDPIINT FROM overview.cognitiveservices.azure.com/';
//$uriBase = 'https://westeurope.api.cognitive.microsoft.com/';


$request_URL = $uriBase . 'vision/v3.0/tag';
//$request_URL = $uriBase . 'vision/v3.1/analyze?visualFeatures=Categories,Description,Tags';

$params = array('language' => 'en');
$request_URL = $request_URL . '?' . http_build_query($params);


$headers = array(
    'Content-Type' => 'application/json',
    'Ocp-Apim-Subscription-Key' => $ocpApimSubscriptionKey,
    'Ocp-Apim-Trace' => true
);


$imageUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg';

$body = json_encode(array(
    'url' => $imageUrl
));

print_r($body);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$request_URL);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$body); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);

curl_close ($ch);

print  $server_output ;

有什么想法吗?

你的$headers错了,下面的代码对我有用。

<?php
// Your code here!
$ocpApimSubscriptionKey = 'b641******47c5558f2b';
$uriBase = 'https://eastus.api.cognitive.microsoft.com/';

$request_URL = $uriBase . 'vision/v3.0/tag';

$params = array('language' => 'en');
$request_URL = $request_URL . '?' . http_build_query($params);


// error code, I don't use
$headers = array(
    'Content-Type' => 'application/json',
    'Ocp-Apim-Subscription-Key' => $ocpApimSubscriptionKey,
    'Ocp-Apim-Trace' => true
);


$imageUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/3c/Shaki_waterfall.jpg';

$data = array("url" => $imageUrl);

$body = json_encode($data);


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$request_URL);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$body); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
// here is correct usage
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Ocp-Apim-Subscription-Key: b6417ac85*****7c5558f2b'
));
$server_output = curl_exec ($ch);

curl_close ($ch);

print  $server_output ;
?>