php new httprequest,php安装HTTP_Request2及引用介绍(通过HTTP_Request创建微软人脸识别的群组 为例)...

// This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)

include('HTTP/Request2.php');

//add 2017-02-07 subscription key

$key1 = 'xxxxxxx'; //在微软人脸识别处注册的账户对应的face应用api key

//add 2017-02-07 personGroupId

$group_id = 'school_1';

$request = new Http_Request2("https://westus.api.cognitive.microsoft.com/face/v1.0/persongroups/{$group_id}");

$url = $request->getUrl();

########################################################################################

# 记得一定要关闭证书验证,否则会出现如下证书验证失败的错误提示

/*

Fatal error: Uncaught

HTTP_Request2_ConnectionException: Unable to connect to tls://westus.api.cognitive.microsoft.com:443.

Error: stream_socket_client(): unable to connect to tls://westus.api.cognitive.microsoft.com:443 (Unknown error)

stream_socket_client(): Failed to enable crypto stream_socket_client(): SSL operation failed with code 1.

OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:

certificate verify failed in /usr/local/php/lib/php/HTTP/Request2/Adapter/Socket.php on line 332

*/

########################################################################################

$request->setConfig(['ssl_verify_peer'=>FALSE,'ssl_verify_host'=>FALSE]);

########################################################################################

$headers = array(

// Request headers

'Content-Type' => 'application/json',

'Ocp-Apim-Subscription-Key' => $key1,

);

$request->setHeader($headers);

$parameters = array(

// Request parameters

'personGroupId'=>$group_id

);

$url->setQueryVariables($parameters);

$request->setMethod(HTTP_Request2::METHOD_PUT);

// Request body

// add body

$body = '{"name":"group1", "userData":"group1 userData test"}';

$request->setBody("{$body}");

try

{

$response = $request->send();

echo $response->getBody();

}

catch (HttpException $ex)

{

echo $ex;

}

?>

以下是通过HTTP_Request创建人脸识别群组操作成功与否返回的提示信息(出现如下状态信息,就说明HTTP_Request成功请求了人脸识别接口,到此HTTP_REQUEST2的使用介绍完毕)

Response 200

A successful call returns an empty response body.

Response 400

Error code and message returned in JSON:

Error Code

Error Message Description

BadArgument

'name' is too long.

BadArgument

'userData' is too long.

BadArgument

Bad and unrecognizable JSON body.

BadArgument

Person group ID is invalid. Valid format should be a string composed by numbers, English letters in lower case, '-', '_', and no longer than 64 characters.

application/json

{

"error":{ "code":"BadArgument", "message":"'name' is too long."}}

Response 401

Error code and message returned in JSON:

Error Code

Error Message Description

Unspecified

Invalid subscription Key or user/plan is blocked.

application/json

{

"error":{ "code": "Unspecified", "message": "Access denied due to invalid subscription key. Make sure you are subscribed to an API you are trying to call and provide the right key."}}

Response 403

application/json

{

"error":{ "statusCode": 403, "message": "Out of call volume quota. Quota will be replenished in 2.12 days."}}

Response 409

Error code and message returned in JSON:

Error Code

Error Message Description

PersonGroupExists

Person group already exists.

ConcurrentOperationConflict

Concurrent operation conflict on resource.

application/json

{

"error":{ "code":"PersonGroupExists", "message":"Person group 'sample_group' already exists."}}

Response 415

Unsupported media type error. Only "application/json" is valid for this API.

application/json

{

"error":{ "code":"BadArgument", "message":"Invalid Media Type"}}

Response 429

application/json

{

"error":{ "statusCode": 429, "message": "Rate limit is exceeded. Try again in 26 seconds."}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值