从 PHP 中执行的 curl 获取输出

Get output from curl executed in PHP

我正在使用 sonicAPI 尝试上传文件。出于某种原因,我不能在 php 中使用 curl 来执行此操作,因为它们的 api 需要标志 -F 来模拟从表单发布....((HTTP)这让 curl 模拟了一个填写好的表单其中用户按下了提交按钮)

总之。我必须使用

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

上传文件。引号中的命令在直接输入 shell 时有效。它 returns 一个 xml 字符串。但是,当我 var_dump($output) 时,我只是得到了 'loading' 信息,而这些信息在实际 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 字符串?该文件确实已成功上传,因此应该有一个 xml 响应。 或者,如果没有,我如何通过 php curl 模拟表单发布?

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

就用CURLOPT_POSTFIELDS,等价的PHPcurl_代码是

$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);