【问题标题】:Issue using PHP cURL with the Streamable API将 PHP cURL 与 Streamable API 一起使用时出现问题
【发布时间】:2017-07-05 17:14:44
【问题描述】:

我希望使用带有 cURL 的 Streamable API 和 PHP 来构建视频上传。 https://streamable.com/documentation#upload-video-file

我想要完成的是:

  1. 用户填写信息表并从他们的计算机/设备中选择要上传的视频文件
  2. 提交表单,PHP 从那里处理它以与 Streamable API 对话,通过表单将视频上传到我的 Streamable 帐户,然后从 Streamable 返回短代码,让我将其其余信息存储在 MySQL 数据库中

我已经成功地尝试通过终端使用 curl 命令。但是,我在通过 php 表单提交将其关闭时遇到了问题。

这是我在终端中使用的命令上传的示例,该命令有效:

curl https://api.streamable.com/upload -u my_email:my_pass -F file=@path/to/file.mp4

借助大量在线帮助,我有了一个非常简单的 cURL 脚本。我想你可以说我对使用 cURL 很陌生。

$url = 'https://api.streamable.com/upload -u my_email.com:my_pass -F file=@path/to/file.mp4';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);

这样,我收到了 HTTP 400 错误。 https://streamable.com/documentation#errors

.. 400 范围内的代码表示客户端错误...

我想这就是我在这里搞砸的原因?

我尝试过这种方式,但我得到了同样的错误。

$pass = 'my_email:my_pass';
$postFields = array(
 'file' => '/path/to/file.mp4',
 'title' => 'Example Title'
);
$url = 'https://api.streamable.com/upload';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, $pass);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);

我使用print_r(curl_getinfo($ch)); 来查看正在发生的事情,这就是它吐出的内容 - 也许这对一些帮助有用:

Array ( [url] => https://api.streamable.com/upload -u my_email:my_pass -F file=@/Path/to/file.mp4 [content_type] => [http_code] => 400 [header_size] => 66 [request_size] => 277 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.36033 [namelookup_time] => 0.00138 [connect_time] => 0.082871 [pretransfer_time] => 0.283219 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => 0 [upload_content_length] => 275 [starttransfer_time] => 0.36031 [redirect_time] => 0 [redirect_url] => [primary_ip] => xx.xx.xxx.xxx [certinfo] => Array ( ) [primary_port] => 443 [local_ip] => 192.168.0.1 [local_port] => 57288 )

【问题讨论】:

    标签: php curl


    【解决方案1】:

    您需要在此处更改几件事:

    • CURLOPT_SSL_VERIFYPEER 设置为false。它可用于验证对等方的证书。如果我们将其指定为 false,它将接受任何服务器(对等)证书。

      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      

      这是关于打开和关闭CURLOPT_SSL_VERIFYPEER 含义的好读物,If CURLOPT_SSL_VERIFYPEER is false, is the data transfer no longer secure?

    • CURLOPT_POSTFIELDS 用于指定我们希望通过 POST 请求提交的完整数据。应使用http_build_query() 函数将$postFields 数组转换为URL 编码的查询字符串,以便将其作为application/x-www-form-urlencoded 发送。

      curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postFields));
      

    【讨论】:

    • 是否值得在此处添加关于关闭证书验证的安全隐患的说明?这对每个人来说可能并不明显。
    • @halfer 啊,好主意,我也应该补充一下。给我一点时间。编辑:更新答案。
    • 感谢您的洞察力!我已经进行了上面引用的更改(注意安全问题),但它给了我相同的结果。我不再使用 POSTFIELDS 选项,而是直接使用了$url 选项。
    • $url = str_replace(' ','%20','https://api.streamable.com/upload -u my_email:my_pass -F file=@/Path/to/file.mp4'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $server_output = curl_exec ($ch); print_r(curl_getinfo($ch)); print_r(curl_error($ch)); print_r($server_output); //this actually displays the Streamable 404 on return page? curl_close ($ch); 现在,当我在网上阅读有关 URL 空白时,使用 str_replace 时出现 404 错误。
    • 输出打印流式 404 错误的屏幕截图 -> link
    【解决方案2】:

    试试这个:

    $fields = array("file"=>curl_file_create("video.mp4"));
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.streamable.com/upload");
    curl_setopt($ch, CURLOPT_USERPWD, "email:pass");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );
    
    $result = curl_exec($ch);
    

    我认为 curl_file_create 成功了,似乎使用 @fileName 不再适用于 PHP 5.5+

    【讨论】:

      猜你喜欢
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 2011-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-26
      相关资源
      最近更新 更多