使用 php 发送时,Azure Face api 返回 401

标签 azure api face

https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236/console

当使用我的 api key 通过此处发送请求时,响应成功返回。

用php通过cURL发送时,返回401

$request_url = 'https://westus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&recognitionModel=recognition_01&returnRecognitionModel=false&detectionModel=detection_01';
$header  = array('Ocp-Apim-Subscription-Key'=> '{sub_key}', 'Content-Type' => "application/json");
$test_data = array('url' => '{image_url}');

$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_URL, $request_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POSTFIELDS, $test_data);

$result = curl_exec($curl);

请告诉我哪里做错了

最佳答案

我认为您设置标题的方式是错误的。这是我的成功示例:

<?php
    $request_url = 'https://southeastasia.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&recognitionModel=recognition_01&returnRecognitionModel=false&detectionModel=detection_01';
    $headers      = [
           "Ocp-Apim-Subscription-Key:1fb1105*********92c6eb8b",
           "Content-Type:application/json"
    ];


    $test_data = array('url' => 'https://storagetest789.blob.core.windows.net/pub/Untitled.png');

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($curl, CURLOPT_URL, $request_url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($test_data));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);


    $strResponse = curl_exec($curl);
    $curlErrno   = curl_errno($curl);
    if ($curlErrno) {
           $curlError = curl_error($curl);
           throw new Exception($curlError);
    }

    $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);

    print_r($http_status."\n");
    print_r($strResponse."\n");  
?>

结果:

enter image description here

关于使用 php 发送时,Azure Face api 返回 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57154095/

相关文章:

facebook - 通过 Facebook API 发送私有(private)消息

iOS 11/12 : How to fetch list of “People Album” from Photos. 应用程序

python-2.7 - 无法从视频中检测人脸并将其保存为图像

git - 我应该在构建步骤中使用 `git push --force`

c# - 检测到多个匹配标记?

azure - 将照片从 Windows 8 上传到 Azure Blob 存储

c - mariadb c api windows undefined reference to

HTML5 History API 实际 URL 不存在

c++ - OpenCV predict() 与 detectMultiScale()

Azure AD SPN 和 UPN?