【问题标题】:azure face api return 401 when sent with php使用 php 发送时,azure face api 返回 401
【发布时间】:2019-07-23 11:18:15
【问题描述】:

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

当使用我的 api 密钥通过这里发送请求时,响应成功返回。

用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);

请告诉我哪里做错了

【问题讨论】:

  • 嗨。你是在本地主机上测试这个吗?

标签: azure api face


【解决方案1】:

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

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

结果:

【讨论】:

  • 你太棒了!
猜你喜欢
  • 2016-04-05
  • 1970-01-01
  • 2019-03-14
  • 1970-01-01
  • 2013-09-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-25
  • 2019-11-05
相关资源
最近更新 更多