代码之家  ›  专栏  ›  技术社区  ›  E.SK

从PHP中执行的curl中获取输出

  •  0
  • E.SK  · 技术社区  · 9 年前

    我正在使用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);
    

    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模拟表单发布?

    2 回复  |  直到 9 年前
        1
  •  0
  •   hanshenrik    9 年前

    只需使用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);
    
        2
  •  0
  •   Ethan SK    9 年前