认知服务-错误创建FaceList和PersonGroup

时间:2018-10-29 00:46:01

标签: php azure azure-cognitive-services

您能帮助我吗,我正在使用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中运行此脚本,它给了我以下错误

{“错误”:{“代码”:“ BadArgument”,“消息”:“请求正文无效。”}}

能告诉我我在做什么错吗

1 个答案:

答案 0 :(得分: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;
}

?>