I am trying to detect face in image, using Mircosoft Face API.
$proxy = 'myProxy';
$img = 'http://example.com/489999.JPG';
$post_string = '{"url":"' . $img . '"}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://westeurope.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,gender,emotion&recognitionModel=recognition_01&returnRecognitionModel=false&detectionModel=detection_01');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string );
$headers = array();
$headers[] = 'Ocp-Apim-Subscription-Key: ****************';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE);
}
curl_close($ch);
echo "<pre>" .
json_encode(json_decode($result), JSON_PRETTY_PRINT) . "</pre>";
My code works well. But I would need to pass image using stream upload instead of providing public URL. I found many examples of how to do that. But I don't know, how to combine it with other headers I need to send.
I have no proxy on my side, so I removed it in code. Pls try the code below ,I have tested on my side and it works for me :
<?php
$imagePath = '<path of your image>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://westeurope.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,gender,emotion&recognitionModel=recognition_01&returnRecognitionModel=false&detectionModel=detection_01');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($imagePath));
$headers = array();
$headers[] = 'Ocp-Apim-Subscription-Key: <sub key >';
$headers[] = 'Content-Type: application/octet-stream';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE);
}
curl_close($ch);
echo "<pre>" .
json_encode(json_decode($result), JSON_PRETTY_PRINT) . "</pre>";
?>