当我尝试使用CURL请求从图像中检测人脸及其属性时,它总是返回以下错误代码
{"code":"BadArgument",“message”:“无效的媒体类型”}。
在使用他们的测试控制台测试人脸检测时,它成功地从特定的image.Here返回了人脸属性是我的代码
$query_params = array('analyzesFaceLandmarks' => 'true',
'analyzesAge' => 'true',
'analyzesGender' => 'true',
'analyzesHeadPose' => 'true',
'subscription-key'=> 'my subscription key'
);
$params = "";
$sep = '';
foreach ($query_params as $key => $value) {
$params .= $sep.$key.'='.$value;
$sep = '&';
}
$API_Endpoint = "https://api.projectoxford.ai/face/v0/detections?".$params;
$img_arr = array('url'=>'{remote file path}');
$data = json_encode($img_arr);
$headers = array();
$headers[] = 'Content-Type:application/json';
$headers[] = 'Content-Length:'.strlen($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
print_r('<pre>');
print_r($result);如果有人知道这个问题的原因,请尽快帮助我。
发布于 2015-11-10 03:39:06
有点晚了,但这里有一个修改后的工作示例。我认为你遇到的问题是它是CURLOPT_HTTPHEADER,而不是CURLOPT_HEADER (在这种情况下)。
define( 'API_BASE_URL', 'https://api.projectoxford.ai/face/v0/detections?' );
define( 'API_PRIMARY_KEY', 'YOUR KEY HERE' );
$img = 'YOUR IMAGE URL HERE';
$post_string = '{"url":"' . $img . '"}';
$query_params = array(
'analyzesFaceLandmarks' => 'true',
'analyzesAge' => 'true',
'analyzesGender' => 'true',
'analyzesHeadPose' => 'true',
);
$params = '';
foreach( $query_params as $key => $value ) {
$params .= $key . '=' . $value . '&';
}
$params .= 'subscription-key=' . API_PRIMARY_KEY;
$post_url = API_BASE_URL . $params;
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($post_string))
);
curl_setopt( $ch, CURLOPT_URL, $post_url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_string );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$response = curl_exec( $ch );
curl_close( $ch );
print_r( '<pre>' );
print_r( $response );https://stackoverflow.com/questions/30326875
复制相似问题