我们正在尝试使用以下 curl 的cmdline进行转换,效果很好
curl --request POST --data-binary @"/home/project/enrol/my.wav" --header "Content-Type:audio/wav" --header "VsitEmail: [email protected]" --header "VsitPassword: 8270f2824111e04d9278c01a92b388147d9d02e0b50d946d25d00db375ff1282" --header "VsitDeveloperId: 200037" https://siv.voiceprintportal.com/sivservice/api/enrollments
到PHP libcurl,它不断产生内部错误。
请指出我错了。
码
$ch = curl_init();
$path="/home/project/enrol/my.wav";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: audio/wav","VsitEmail: [email protected]","VsitPassword: 8270f2824111e04d9278c01a92b388147d9d02e0b50d946d25d00db375ff1282","VsitDeveloperId: 200037"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt( $ch , CURLOPT_VERBOSE , 0 );
curl_setopt($ch, CURLOPT_POSTFIELDS,array('file' => '@'.$path));
$result = curl_exec($ch);
//close connection
curl_close($ch);
print_r($result);
更新1
$localfile='/home/project/enrol/my.wav';
$url = 'https://siv.voiceprintportal.com/sivservice/api/enrollments';
$ch = curl_init();
$fields = array('file' => '@' .$localfile);
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
//curl_setopt($resource, CURLOPT_HTTPHEADER, $header);
curl_setopt($resource, CURLOPT_HTTPHEADER, array("Content-Type:
audio/wav","VsitEmail: [email protected]","VsitPassword: 8270f2824111e04d9278c01a92b388147d9d02e0b50d946d25d00db375ff1282","VsitDeveloperId: 200037"));
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($resource, CURLOPT_POST, 1);
curl_setopt($resource, CURLOPT_BINARYTRANSFER, true);
curl_setopt($resource, CURLOPT_POSTFIELDS, $fields);
//$result = json_decode(curl_exec($resource));
$result=curl_exec($resource);
curl_close($resource);
echo "The result is";
print_r($result);
最佳答案
检查以下代码
$url = 'http://google.com';
$header = array('Content-Type: multipart/form-data');
$fields = array('file' => '@' . $_FILES['file']['tmp_name'][0]);
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
curl_setopt($resource, CURLOPT_HTTPHEADER, $header);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($resource, CURLOPT_POST, 1);
curl_setopt($resource, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($resource);
curl_close($resource);