0

I have this application where from a web page the user uploads a file, what i need to do is that the application sends this file to another API.

Now i don't care what happens or what response i get after doing the cURL request because it takes too long to process. i only notify the user that his/her file was uploaded correctly.

my code:

 curl_setopt_array( $ch, [ CURLOPT_VERBOSE=>true, CURLOPT_RETURNTRANSFER => true, CURLOPT_URL => $this->endpoint, CURLOPT_POSTFIELDS => $this->payload, ] ); curl_exec($ch); 

$this->endpoint contains the url and $this->payload the file received. From the API i sent that file i get the message that no file was sent or the file is in a incorrect format. My guess is that i'm not sending the file correctly when executing the command. How to i can send that file?

15
  • coderwall.com/p/fck2ta/how-to-send-files-via-curl-in-php Dont use exec please Commented Oct 25, 2019 at 21:16
  • @НиколайЛубышев I already read that, the file itself it's not on the server but received from another web request Commented Oct 25, 2019 at 21:17
  • 1
    Why don't you use the built-in curl functions of PHP? You can't use curl_getinfo() with exec(). Commented Oct 25, 2019 at 21:21
  • Content-Type: application/json ? file bypass with "Content-Type: octet/stream" its first Second? try save file and after that send it. Next step send from memory be stream php-functions, AND DO NOT exec() )))) Commented Oct 25, 2019 at 21:22
  • 1
    Your explanation of why you're doing it this way doesn't make sense. You're not running the command in the background, so exec() waits for the response. Commented Oct 25, 2019 at 21:32

1 Answer 1

2

Use the built-in curl functions.

$ch = curl_init($this->endpoint); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($this->payload), CURLOPT_HTTPHEADER => [ "Content-type: application/json", "Accept: application/json" ] ]); if (curl_exec($ch)) { return [ 'code' => 200, 'success' => True, 'message' => curl_getinfo($cmd), ]; } else { return [ 'code' => curl_getinfo($ch, CURLINFO_RESPONSE_CODE), 'success' => false, 'message' => "Curl failed" ]; } 
Sign up to request clarification or add additional context in comments.

1 Comment

that will wait until the entire upload is complete before returning. this guy probably needs fastcgi_finish_request()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.