PHP uses Microsoft cognitive services face API

Source: Internet
Author: User

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
    • Composer.json file
 {   "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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.