Search code examples
phpcurlazure-cognitive-services

Cognitive Services // AI Vision Tag over PHP


my PHP Request to tag some images doesnt work, i get an error.

{"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."}}

Here is my PHP Script with 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 ;

Any Ideas?


Solution

  • Your $headers is wrong, below code is works for me.

    enter image description here

    <?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 ;
    ?>