php - 认知服务 - 创建 FaceList 和 PersonGroup AZURE 时出错

标签 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 中运行这个脚本,它给了我以下错误

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

请告诉我我做错了什么

最佳答案

根据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;
}

?>

关于php - 认知服务 - 创建 FaceList 和 PersonGroup AZURE 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53037409/

相关文章:

azure - 新的 ms botbuilder 直线语音是否适合调用中心场景?

php - php 字符串中第二个字符小写

php - 让 HABTM 关系在 CakePHP 中独一无二

angularjs - 如何通过 Azure Verizon Premium CDN 使用来自 Azure Blob 的路由为 Angular 2 SPA 提供服务

azure - 从 Azure Blob 存储中的任何身份验证检查中排除 sig querystring 参数

c# - 更改 Azure AD B2C 注销 URL(将 AzureADB2C/Account/SignedOut 更改为自定义 URL)

php - 如何使用 PHP 和 FFMPEG 分割和转换视频

php - Laravel orderByRaw 正在从联合查询中删除结果

azure - 有谁知道 Azure OpenAI 服务何时可以使用文本图像?

c# - 在 Azure 中检索聊天机器人对话数据