Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hammers's avatar

Forum > Forward file from cURL response directly

Hello, I'm searching a solution to forward directly the answer of my cURL request. We have 2 API: 1 public (Laravel 5.8) and 1 private (Java, only local access) I make some verification, security, etc. in the public API. So when i've got a request, after verification, i send the request to the private API. The private API create a file from some files and return a file that correspond to params asked. I need to forward this audio/video files from the private API. But for the moment, i need to stock the file on the aol mail login PHP server before send it as an answer. I want to skip this step and forward the answer from my cURL directly. Is it possible? And if yes, how I could do it?

Here is my actual code: ` $ch = curl_init(); // build URL $url = $this->url.'stream-chunk?stream='.$stream->id_audio_stream_info.'&from='.$from; $url .= !is_null($to) ? '&to='.$to : ''; $url .= !is_null($bitRate) ? '&bit-rate='.$bitRate : ''; $url .= !is_null($quality) ? '&quality='.$quality : ''; $url .= !is_null($format) ? '&format='.$format : ''; $url .= !is_null($mode) ? '&mode='.$mode : ''; $url .= !is_null($sampleRate) ? '&sample-rate='.$sampleRate : '';

curl_setopt($ch,CURLOPT_URL,$url); $headers = [ 'Accept: /', 'Content-Type: application/json' ]; curl_setopt($ch, CURLOPT_HTTPHEADER,$headers); curl_setopt($ch,CURLOPT_FAILONERROR, true); curl_setopt($ch, CURLINFO_HEADER_OUT, true); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

$result = curl_exec($ch);

if ($result === FALSE) { $e = curl_error($ch); Log::error($url); Log::error($e); $error_code = curl_getinfo($ch , CURLINFO_HTTP_CODE); $error_message = $e; $response = [ 'msg'=>$error_message ]; $error_code = $error_code == 0 ? 400 : $error_code; curl_close($ch); return response()->json($response,$error_code); }

$code = curl_getinfo($ch, CURLINFO_HTTP_CODE ); $info = curl_getinfo($ch); curl_close($ch);

if ($code === 204) { return response()->json(['msg'=>'No Content'],$code); }

$mime_type = $info['content_type']; $file_size = $info['size_download'];

$response = response($result,200); $response->header('Accept','/'); $response->header('Content-Type',$mime_type); $response->header('Content-Length',$file_size); return $response; `

0 likes
1 reply
s4muel's avatar

🙏 format the code (use three backticks ``` instead of one ` and put them on separate lines)

Please or to participate in this conversation.