The following main introduction is based on the PHP language, based on the Guzzle class library, calling Microsoft's latest introduction of cognitive services: face recognition. Lab Environment:
- Ide:eclipse for PHP Developers
- Version:neon.1 Release (4.6.1)
- Server:wampserver Version 2.5
- Httpclient:guzzle
1. Install guzzle using composer
{ "require": { "guzzlehttp/guzzle": "~6.0" }}
- Composer.phar Download
- Open the console directive and run the following command in the project directory to complete the installation of the package
php composer.phar install
2. Certificate Installation
Note : The default HTTP client tools generally require certificate authentication, and SSL certificate authentication errors are generally reported if the corresponding certificate is not imported.
- Download the certificate locally, and save it to the appropriate location.
- In order for the certificate to take effect, modify the php.ini accordingly, as follows:
curl.cainfo =<filepath>/cacert.pem
3. PHP code example
<?phpuse 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. Example of running results
[{"faceId":"b22efa8f-0e34-4344-8346-ca8b79d29a27","faceRectangle":{"top":181,"left":184,"width":257,"height":257}}]
5. Read local images
<?phpuse 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. More references
- Access to key can be obtained by logging into the website of the century Internet application and creating cognitive services in the new version of portal.
- Cognitive Services Restapi Reference
PHP uses Microsoft cognitive services face API