简体   繁体   English

卷曲到php以发布json和音频

[英]curl to php to post a json and audio

I'm trying to convert a curl command to be used in a php script 我正在尝试转换curl命令以在php脚本中使用

   curl -k -F "request={'timezone':'America/New_York','lang':'en'};type=application/json" -F "voiceData=@d8696c304d09eb1.wav;type=audio/wav" -H "Authorization: Bearer x" -H "ocp-apim-subscription-key:x" "http://example.com"

and here is my php script 这是我的PHP脚本

<?
$data = array("timezone" => "America/New_York", "lang" => "en", "voiceData" => "d8696c304d09eb1.wav");                                                                    
$data_string = json_encode($data);                                                                                   
$ch = curl_init('https://example.com');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-type: application/json',
   'Authorization: Bearer x',
   'ocp-apim-subscription-key:x')
);
$result = curl_exec($ch);
?>

I understand that the send audio file bit is not right but i cant find an example how to post it. 我了解发送音频文件的位不正确,但我找不到如何发布它的示例。

I have edited this in response to the post fields but if I include $ch i get no output if don't include the output complains that no post request. 我已经编辑了此内容以响应发布字段,但是如果我包含$ ch,则不输出,如果不包含输出会抱怨没有发布请求。 Any ideas? 有任何想法吗?

<?
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$ch = curl_init('https://example.com');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Content-type: application/json',
   'Authorization: Bearer x',
   'ocp-apim-subscription-key:x')
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 
    array('request' => json_encode(array('timezone' => 'America/New_York', 'lang' => 'en')),
          'voicedata' => new CURLFile("d8696c304d09eb1.wav")
    )
);
$result = curl_exec($ch);
echo $result;
?>

You're missing the request= in your POST fields. 您在POST字段中缺少request= Also, sending files using @filename is deprecated, you should use the CURLFile class. 另外,不建议使用@filename发送文件,您应该使用CURLFile类。

curl_setopt($ch, CURLOPT_POSTFIELDS, 
    array('request' => json_encode(array('timezone' => 'America/New_York', 'lang' => 'en')),
          'voicedata' => new CURLFile("d8696c304d09eb1.wav")
    )
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM