‘Ocp-Apim-Subscription-Key‘ = ‘bd8e4ce12f444c639ac9c214d70ac

时间:2022-02-20 06:46:18

下面主要介绍基于PHP语言,基于guzzle类库,挪用微软最新推出的认知处事:人脸识别。 尝试环境: 1. 使用composer安置Guzzle

composer.json文件

{ "require": { "guzzlehttp/guzzle": "~6.0" } } php composer.phar install 2. 证书安置

注意:默认的http客户端工具一般到需要证书的认证,,如果没有导入相应的证书一般会报SSL证书认证错误。

curl.cainfo =<filepath>/cacert.pem 3. PHP代码示例 <?php use GuzzleHttp\Psr7\Request; use GuzzleHttp\Client; require_once ‘vendor\autoload.php‘; $client = new Client(); $headers = [‘Content-Type‘ => ‘application/json‘,‘Ocp-Apim-Subscription-Key‘ => ‘{key}‘]; $body = ‘{"url":"{image‘s url}"}‘; $request = new Request(‘POST‘,‘https://api.cognitive.azure.cn/face/v1.0/detect‘, $headers , $body); $response = $client->send($request); echo $response->getBody(); ?> 4. 运行功效示例 [{"faceId":"b22efa8f-0e34-4344-8346-ca8b79d29a27","faceRectangle":{"top":181,"left":184,"width":257,"height":257}}] 5. 读取本舆图片 <?php use GuzzleHttp\Psr7\Request; use GuzzleHttp\Client; require_once ‘vendor\autoload.php‘; $client = new Client(); $headers = [‘Content-Type‘ => ‘application/octet-stream‘,‘Ocp-Apim-Subscription-Key‘ => ‘bd8e4ce12f444c639ac9c214d70ac72c‘]; $myfile = fopen("tt.jpg", "r") or die("Unable to open file!"); $request = new Request(‘POST‘,‘https://api.cognitive.azure.cn/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age‘, $headers , $myfile); $response = $client->send($request); echo $response->getBody(); fclose($myfile); ?> 6. 更多参考