简体   繁体   English

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

[英]Sending Media Files using CURL - Laravel PHP

I am posting data(including a media file (.wav)) from my app to an API with curl. 我正在将数据(包括媒体文件(.wav))从我的应用发布到带有curl的API。 When submitting my data, i check for the data including the mediafile submitted in my API. 提交数据时,我会检查数据,包括在我的API中提交的媒体文件。 From the response i get from my API, see below 从我从API获得的响应中,请参见下文

Response 响应

  {"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

In the response, the file is being retrieved as well but when i check for the file using $request->hasFile('file') or $request->file('file') , I get false and null respectively. 在响应中,也正在检索文件,但是当我使用$ request-> hasFile('file')或$ request-> file('file')检查文件时 ,分别得到falsenull

Can someone let me know why this is happening in my code please ? 有人可以让我知道为什么这会在我的代码中发生吗?

Controller 调节器

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 APIController

public function campaign(Request $request)

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

}

To be honest, I'd use Guzzle to hide the details of cURL requests in PHP. 老实说,我将使用Guzzle在PHP中隐藏cURL请求的详细信息。 The way PHP's cURL extension handles file transfers changed a couple of years ago, which broke a lot of legacy code at the company I was working for. PHP的cURL扩展名处理文件传输的方式在几年前发生了变化,这打破了我所在公司的许多旧代码。 By using a third-party wrapper like Guzzle, you can let the Guzzle developers worry about changes in the underlying extension - all you need to do is keep your Guzzle package up to date. 通过使用像Guzzle这样的第三方包装器,您可以让Guzzle开发人员担心基础扩展的更改-您所需要做的就是保持Guzzle软件包为最新。

PHP - Why Use Guzzle Instead of cURL? PHP-为什么使用Guzzle而不是cURL?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM