如何通过PHP CURL(Microsoft Azure API)发送(上载)文件

如何通过PHP CURL(Microsoft Azure API)发送(上载)文件,php,azure,curl,microsoft-cognitive,Php,Azure,Curl,Microsoft Cognitive,我正在尝试使用Mircosoft face API检测图像中的人脸 $proxy='myProxy'; $img='1http://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=tr

我正在尝试使用Mircosoft face API检测图像中的人脸

$proxy='myProxy';
$img='1http://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,性别、情绪和识别模型=识别_01&返回识别模型=错误和检测模型=检测_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订阅密钥:**************”;
$headers[]=“内容类型:application/json”;
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
$result=curl\u exec($ch);
if(旋度误差($ch)){
echo“Error:”.curl_Error($ch);
echo curl_getinfo($ch,CURLINFO_HTTP_代码);
}
卷曲关闭($ch);
“回声”。
json_编码(json_解码($result),json_打印)。"";
我的代码运行良好。但我需要使用流上传来传递图像,而不是提供公共URL。
我找到了很多这样做的例子。但是我不知道如何将它与我需要发送的其他头文件结合起来。

我这边没有代理,所以我在代码中删除了它。请尝试下面的代码,我已经在我这方面进行了测试,它适合我:


结果:

欢迎!快乐编码!
<?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>";

?>