0

I am posting data(including a media file (.wav)) from my app to an API with curl. When submitting my data, i check for the data including the mediafile submitted in my API. From the response i get from my API, see below

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.

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

public function campaign(Request $request)

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

}
RoboPHP
  • 410
  • 4
  • 12

1 Answers1

1

To be honest, I'd use Guzzle to hide the details of cURL requests in PHP. 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. 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.

PHP - Why Use Guzzle Instead of cURL?

Diggy Dude
  • 19
  • 2