2017-08-03 131 views
0

我正在使用sonicAPI尝试上载文件。出于某种原因,我无法在php中使用curl来做到这一点,因为他们的api需要模拟从表单发布的标志-F((HTTP))这可以让curl模拟用户按下提交的填充表单按钮)从PHP执行curl获取输出

无论如何。我必须使用

exec("$curl https://api.sonicapi.com/file/upload?access_id=$accessId [email protected]/shortaudio.mp3 2>&1", $output, $exit); 

上传文件。直接输入到shell中时,引号中的命令有效。它返回一个xml字符串。然而,当我var_dump($ output)时,我只是在实际的shell中执行时才得到实际没有显示的'加载'信息。我得到这个

array(4) { 
    [0]=> 
    string(79) " % Total % Received % Xferd Average Speed Time Time  Time Current" 
    [1]=> 
    string(77) "         Dload Upload Total Spent Left Speed" 
    [2]=> 
    string(158) " 
    0  0 0  0 0  0  0  0 --:--:-- --:--:-- --:--:--  0 
100 17008 100 249 100 16759 393 26489 --:--:-- --:--:-- --:--:-- 26517" 
    [3]=> 
    string(249) "" 
} 

如何获取应返回的XML字符串?该文件确实上传成功,所以defo应该是一个XML响应。 或者,如果不是,我该如何通过php curl来模拟表单发布?

+0

你应该能够(5.5+)使用要做到这一点在PHP [CURLFile](http://php.net/manual/en/curlfile.construct.php) – iainn

+0

嗯...仍然有400错误。使用这个$ cfile = curl_file_create(“shortaudio.mp3”,'multipart/form-data','shortaudio'); –

回答

0

只使用CURLOPT_POSTFIELDS,等效PHP curl_代码

$ch = curl_init ('https://api.sonicapi.com/file/upload?access_id=' . $accessId); 
curl_setopt_array ($ch, array (
     CURLOPT_POST => true, 
     CURLOPT_POSTFIELDS => array (
       'file'=>new CURLFile('shortaudio.mp3') 
     ), 
     CURLOPT_RETURNTRANSFER=>true 
)); 
$output=curl_exec($ch); 
curl_close($ch); 
0

只是使用-o file.xml保存XML响应