【问题标题】:Cognitive Services - Error Create FaceList and PersonGroup AZURE认知服务 - 创建 FaceList 和 PersonGroup 时出错 AZURE
【发布时间】:2018-10-30 08:47:01
【问题描述】:

你能帮我吗?我正在研究使用 azure 服务的认知服务,但我在使用文档中的 forncecido 模型时遇到了一些错误

                <?php
            // This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
            require_once 'HTTP/Request2.php';
            // Replace <Subscription Key> with a valid subscription key.
            $ocpApimSubscriptionKey = '98471c5c832e466688890f6c86f6c88d'; 


            $request = new Http_Request2('https://brazilsouth.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}');
            $url = $request->getUrl();

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

            $request->setHeader($headers);

            $parameters = array(
                // Request parameters
                'faceListId' => 'sahara'
            );

            $url->setQueryVariables($parameters);

            $request->setMethod(HTTP_Request2::METHOD_PUT);

            // Request body
            $request->setBody("{body}");

            try
            {
                $response = $request->send();
                echo $response->getBody();
            }
            catch (HttpException $ex)
            {
                echo $ex;
            }

            ?>

我在 php 中运行这个脚本,它给了我以下错误

{"error":{"code":"BadArgument","message":"请求正文无效。"}}

你能告诉我我做错了什么吗

【问题讨论】:

    标签: php azure azure-cognitive-services


    【解决方案1】:

    根据FaceList - Create API,正文格式为

    {
        "name": "sample_list",
        "userData": "User-provided data attached to the face list."
    }
    

    我们需要通过以下方式发送正文,然后它应该可以工作。

    // Request body
    $request->setBody('{"name":"facelistName","userData":"it is optional"}'); //replace it with your name and userData
    

    如果要引用'HTTP/Request2.php';,我们需要安装http_request2

    pear install http_request2
    

    PEAR包管理器如何安装,请参考link

    演示代码:

    <?php
    // This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
    require_once 'HTTP/Request2.php';
    
    $request = new Http_Request2('https://xxx.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}');
    $url = $request->getUrl();
    
    $headers = array(
        // Request headers
        'Content-Type' => 'application/json',
        'Ocp-Apim-Subscription-Key' => 'xxxxxxxxxx', //replace it with your key
    );
    
    $request->setHeader($headers);
    
    $parameters = array(
        'faceListId' => 'facelistId'
    );
    
    $url->setQueryVariables($parameters);
    
    $request->setMethod(HTTP_Request2::METHOD_PUT);
    
    // Request body
    $request->setBody('{"name":"facelistName","userData":"it is optional"}');//replace it with your name and userData
    
    try
    {
        $response = $request->send();
        echo $response->getBody();
    }
    catch (HttpException $ex)
    {
        echo $ex;
    }
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2021-04-01
      • 2020-10-29
      • 1970-01-01
      • 2019-11-17
      • 1970-01-01
      • 1970-01-01
      • 2020-02-02
      • 2021-07-01
      • 1970-01-01
      相关资源
      最近更新 更多