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

ankur_g's avatar

No response is being returned from a CURL POST call with a .csv file in Laravel 9

I have tried to make the CURL call in the following manner:

$ch = curl_init();
                curl_setopt($ch , CURLOPT_URL , "http://44.193.201.129:8000/summarize");
                curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_HTTPHEADER, [
                    "Content-Type: text/csv"
                ]);
                $file = '/opt/lampp/htdocs/multitenant/public/files/iq_data.csv';
                curl_setopt($ch,CURLOPT_POSTFIELDS, $file);
                $iq_response = curl_exec($ch);
                curl_close($ch);
                $personascoreobject->iq_test_score = $iq_response;
                $personascoreobject->save();

Here the $personascoreobject is the model instance of a table where I am trying to insert the response received from the CURL call in the column iq_test_score. But the response does not return anything . It is returning :

{"error":"500 - Internal server error"}

Whereas when I am separately hit the URL from Postman with the .csv fiIe , I am receiving the response correctly.

Kindly suggest where am I doing the CURL call incorrectly.

0 likes
10 replies
ankur_g's avatar

@vincent15000 Sir there are no logs related to this error when I am dd($iq_response) it is displaying the above error message. That is the URL is returning this. But if I separately send this file through Postman with the same URL, it is returning the result

1 like
ankur_g's avatar

@vincent15000 okay, Sir could you kindly point out if possible that if there is any incorrect statement in the CURL calling statements.

1 like
ankur_g's avatar

@Sinnbeck Okay sir, I am trying with Http client, I will let you know soon

1 like
ankur_g's avatar

@Sinnbeck I have tried using Http Client:

$file = '/opt/lampp/htdocs/multitenant/public/files/iq_data.csv';
                $response = Http::post('http://44.193.201.129:8000/summarize', [
                    'file' => $file
                ]);
                dd($response);

It is still returning the same error, but can I change the content-type of the data that is being sent to text/csv? Then if something changes.

Illuminate\Http\Client\Response {#2174
  #response: GuzzleHttp\Psr7\Response {#2207
    -reasonPhrase: "Internal Server Error"
    -statusCode: 500
    -headers: array:3 [
      "Date" => array:1 [
        0 => "Sat, 18 Feb 2023 12:22:31 GMT"
      ]
      "Content-Type" => array:1 [
        0 => "application/json"
      ]
      "Content-Length" => array:1 [
        0 => "39"
      ]
    ]
    -headerNames: array:3 [
      "date" => "Date"
      "content-type" => "Content-Type"
      "content-length" => "Content-Length"
    ]
    -protocol: "1.1"
    -stream: GuzzleHttp\Psr7\Stream {#2208
      -stream: stream resource @15
        wrapper_type: "PHP"
        stream_type: "TEMP"
        mode: "w+b"
        unread_bytes: 0
        seekable: true
        uri: "php://temp"
        options: []
      }
      -size: null
      -seekable: true
      -readable: true
      -writable: true
      -uri: "php://temp"
      -customMetadata: []
    }
  }
  #decoded: null
  +"cookies": GuzzleHttp\Cookie\CookieJar {#2188
    -cookies: []
    -strictMode: false
  }
  +"transferStats": GuzzleHttp\TransferStats {#2209
    -request: GuzzleHttp\Psr7\Request {#2204
      -method: "POST"
      -requestTarget: null
      -uri: GuzzleHttp\Psr7\Uri {#2194
        -scheme: "http"
        -userInfo: ""
        -host: "44.193.201.129"
        -port: 8000
        -path: "/summarize"
        -query: ""
        -fragment: ""
      }
      -headers: array:4 [
        "Content-Length" => array:1 [
          0 => "72"
        ]
        "User-Agent" => array:1 [
          0 => "GuzzleHttp/6.5.5 curl/7.68.0 PHP/7.4.33"
        ]
        "Content-Type" => array:1 [
          0 => "application/json"
        ]
        "Host" => array:1 [
          0 => "44.193.201.129:8000"
        ]
      ]
      -headerNames: array:4 [
        "content-length" => "Content-Length"
        "user-agent" => "User-Agent"
        "content-type" => "Content-Type"
        "host" => "Host"
      ]
      -protocol: "1.1"
      -stream: GuzzleHttp\Psr7\Stream {#2195
        -stream: stream resource @13
          wrapper_type: "PHP"
          stream_type: "TEMP"
          mode: "w+b"
          unread_bytes: 0
          seekable: true
          uri: "php://temp"
          options: []
        }
        -size: 72
        -seekable: true
        -readable: true
        -writable: true
        -uri: "php://temp"
        -customMetadata: []
      }
    }
    -response: GuzzleHttp\Psr7\Response {#2207}
    -transferTime: 0.006723
    -handlerStats: array:38 [
      "url" => "http://44.193.201.129:8000/summarize"
      "content_type" => "application/json"
      "http_code" => 500
      "header_size" => 127
      "request_size" => 232
      "filetime" => -1
      "ssl_verify_result" => 0
      "redirect_count" => 0
      "total_time" => 0.006723
      "namelookup_time" => 3.7E-5
      "connect_time" => 0.000855
      "pretransfer_time" => 0.000897
      "size_upload" => 72.0
      "size_download" => 39.0
      "speed_download" => 6500.0
      "speed_upload" => 12000.0
      "download_content_length" => 39.0
      "upload_content_length" => 72.0
      "starttransfer_time" => 0.006073
      "redirect_time" => 0.0
      "redirect_url" => ""
      "primary_ip" => "44.193.201.129"
      "certinfo" => []
      "primary_port" => 8000
      "local_ip" => "172.31.63.218"
      "local_port" => 55146
      "http_version" => 2
      "protocol" => 1
      "ssl_verifyresult" => 0
      "scheme" => "HTTP"
      "appconnect_time_us" => 0
      "connect_time_us" => 855
      "namelookup_time_us" => 37
      "pretransfer_time_us" => 897
      "redirect_time_us" => 0
      "starttransfer_time_us" => 6073
      "total_time_us" => 6723
      "appconnect_time" => 0.0
    ]
    -handlerErrorData: 0
  }
}
1 like
Sinnbeck's avatar

@ankur_g are you checking the logs on the server that you are making the request to? I assume that's what is giving the 500 error?

1 like
ankur_g's avatar

@Sinnbeck Sir I have solved it, the CURL call was done incorrectly.

$ch = curl_init();
                curl_setopt_array($ch, array(
                    CURLOPT_URL => "http://44.193.201.129:8000/summarize",
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_ENCODING => "",
                    CURLOPT_MAXREDIRS => 10,
                    CURLOPT_TIMEOUT => 0,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_CUSTOMREQUEST => "POST",
                    CURLOPT_POSTFIELDS => array(''=> new \CURLFILE('/opt/lampp/htdocs/multitenant/public/files/iq_data.csv' , 'text/csv' , 'file')),
                ));
                $iq_response = curl_exec($ch);
                curl_close($ch);

On implementing the above code the response was returning correctly.

1 like
vincent15000's avatar

@ankur_g That's what I said you from the beginning, look at a previous answer from me, I told you that you had a problem with curl. ;)

1 like

Please or to participate in this conversation.