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()
            ]);
        }

}
6
  • $request->hasFile('file') checks if the request has an uploaded file. You appear to be sending a file path, which isn't the same thing - it's just a string, not a file. Commented Feb 1, 2019 at 15:28
  • @ceejayoz, okay. i am basically trying to get the file uploaded. How do i achieve that ? Commented Feb 1, 2019 at 15:31
  • See https://php.net/curl_file_create Commented Feb 1, 2019 at 15:32
  • @ceejayoz, how different is my code from the link you just sent ? Did you take a look at it ? Commented Feb 1, 2019 at 16:09
  • You've got a function_exists('curl_file_create') check. Are you sure it exists on your PHP version? Commented Feb 1, 2019 at 16:10

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.