Reputation: 11
When I try to detect the face and its attributes from an image using CURL request to microsoft`s projectoxford it always return the following error code
{"code":"BadArgument","message":"Invalid Media Type."}.
While testing the face detection using their test console, it successfully returns the face attributes from the particular image.Here is my code
$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);
If anybody know the reason for this problem,please help me as soon as possible.
Upvotes: 0
Views: 1654
Reputation: 89
A bit late, but here is a modified working example. I think the issue you were having is it's CURLOPT_HTTPHEADER - not CURLOPT_HEADER (in this case).
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 );
Upvotes: 1