【问题标题】:Get output from curl executed in PHP从 PHP 中执行的 curl 获取输出
【发布时间】:2017-08-03 16:57:07
【问题描述】:

我正在使用 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 时有效。它返回一个 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 字符串?该文件确实已成功上传,因此应该有一个 xml 响应。 或者,如果没有,我如何通过 php curl 模拟表单发布?

【问题讨论】:

  • 你应该可以在 PHP (5.5+) 中使用CURLFile
  • 嗯...仍然收到 400 错误。使用这个 $cfile = curl_file_create("shortaudio.mp3",'multipart/form-data','shortaudio');

标签: php xml shell curl exec


【解决方案1】:

只要使用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);

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 2022-10-17
      • 2011-09-06
      • 2021-03-27
      • 1970-01-01
      相关资源
      最近更新 更多