如何通过 PHP CURL 发送(上传)文件(Microsoft Azure API)
How to send (upload) file via PHP CURL (Microsoft Azure API)
我正在尝试使用 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>";
我的代码运行良好。但是我需要使用流上传来传递图像,而不是提供 public URL。
我发现了很多如何做到这一点的例子。但我不知道,如何与其他headers我需要发送的结合起来。
我这边没有代理,所以我在代码中删除了它。请尝试下面的代码,我已经在我这边进行了测试,它对我有用:
<?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>";
?>
结果:
我正在尝试使用 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>";
我的代码运行良好。但是我需要使用流上传来传递图像,而不是提供 public URL。 我发现了很多如何做到这一点的例子。但我不知道,如何与其他headers我需要发送的结合起来。
我这边没有代理,所以我在代码中删除了它。请尝试下面的代码,我已经在我这边进行了测试,它对我有用:
<?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>";
?>
结果: