使用CURL发送媒体文件-Laravel PHP

时间:2019-02-01 15:12:35

标签: php laravel

我正在将应用程序中的数据(包括媒体文件(.wav))发布到带有curl的API。提交数据时,我会检查数据,包括在我的API中提交的媒体文件。从我从我的API得到的响应中,请参见下文

回复

  {"status":"success","media":false,"data":{"message":"Media Campaign","recipient":["34505140704"],
"file":{"name":"\/Users\/path\/to\/folder\/public\/Voice\/aaaah.wav","mime":null,"postname":null}}}true

在响应中,也正在检索文件,但是当我使用 $ request-> hasFile('file')或$ request-> file('file')检查文件时,我分别得到falsenull

有人可以让我知道为什么这会在我的代码中发生吗?

控制器

public function test()
{
   $file_name_with_full_path = '/Users/path/to/folder/public/Voice/aaaah.wav';
    if(function_exists('curl_file_create'))
    {
      $cFile = curl_file_create($file_name_with_full_path);
    }
    else
    {
      $cFile = '@' . realpath($file_name_with_full_path);  
    }
    $post = array('message' => 'Media Campaign', 'recipient' => ['34505140704'],'file' => $cFile);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
    $result=curl_exec ($ch);
    curl_close ($ch);
}

APIController

public function campaign(Request $request)

{
     if (($request->get('message')) {
            return response()->json([
                'status' => 'success',
                'data' => $request->all()
            ]);
        }

}

1 个答案:

答案 0 :(得分:1)

说实话,我将使用Guzzle在PHP中隐藏cURL请求的详细信息。 PHP的cURL扩展名处理文件传输的方式在几年前发生了变化,这打破了我所在公司的许多旧代码。通过使用像Guzzle这样的第三方包装器,您可以让Guzzle开发人员担心基础扩展的更改-您所需要做的就是保持Guzzle软件包为最新。

PHP - Why Use Guzzle Instead of cURL?