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

TheWizard's avatar

Send file using cURL from Laravel request

I have a form with string inputs and two files (extensions .cert and .key) and I need to send this input to a nodejs API. The programmer that build the API send me the structure in a Postman project. Using Postman, the service works.

Here is my code at my ClienteController


<?php

 if($request->hasFile('cer') && $request->hasFile('key'))
        {
            //los guarda en binario
            $certificado = $request->file('cer');
            $key = $request->file('key');

            $ruta_certificado = $certificado->storeAs('certs', $certificado->getClientOriginalName());
            $ruta_archivoKey  = $key->storeAs('keys', $key->getClientOriginalName());

            $certFile = new \CURLFile($ruta_certificado);
            $keyFile = new \CURLFile($ruta_archivoKey);

            $certFile->setPostFilename($certificado->getClientOriginalName());
            $certFile->setMimeType('cer');

            $keyFile->setPostFilename($archivoKey->getClientOriginalName());
            $keyFile->setMimeType('key');

     $headers = array(
                'Content-Type: application/json',
                'x-api-key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
            );
            
            $postfields = array(
                //"certificado"  => $request->cer, 
                //"llavePrivada" => $request->key, 
                "password"     => "1234567", 
                "rfc"          => $request->RFC, 
                "razon"        => $request->Razon_Social
            );

            $ch = curl_init(env('API_STAMP_URL').'/v1/emisor/create');
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);            

            $response = curl_exec($ch);
        
            return $response;

        } 

The thing is, I can't find a decent tutorial about sending this data form. Can anybody help me? thanks a lot!

0 likes
2 replies

Please or to participate in this conversation.