0

Executing this curl command from terminal is working and i am getting back my results.

curl -F "file=@/var/www/html/uploads/fb5bc34e1f0f03c759e92010f6a1a302_modified.csv" http://10.0.0.106:8089/uploadAndMatch/ -o ~/out.txt

But when trying it from PHP with the following code I am getting 400 httpcode:

    $request = curl_init('http://10.0.0.106:8089/uploadAndMatch/');
    curl_setopt($request, CURLOPT_POST, true);
    curl_setopt($request,
    CURLOPT_POSTFIELDS,
    array(
        'file' => '@/var/www/html/uploads/fb5bc34e1f0f03c759e92010f6a1a302_modified.csv'
    ));
    curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
    echo curl_exec($request);
    $httpcode = curl_getinfo($request, CURLINFO_HTTP_CODE);
    echo $httpcode;
    curl_close($request);

I also tried to add a header :

$header = array('Content-Type: multipart/form-data');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);

But it's still the same error! 400 bad request Any thoughts?

It wasn't a debugging problem, it's because the changes in php5.5+

Yamen Nassif
  • 2,416
  • 2
  • 24
  • 48
  • can you compare the actual requests (curl, php+curl) with wireshark? do you have access to the servers code/logs? – BNT Nov 06 '17 at 12:51
  • also, maybe your server does not support form-data as stated [here](https://stackoverflow.com/a/28411/7926064) so maybe try [http_build_query](http://php.net/manual/en/function.http-build-query.php) – BNT Nov 06 '17 at 13:00
  • Possible duplicate of [Php - Debugging Curl](https://stackoverflow.com/questions/3757071/php-debugging-curl) – Nic3500 Nov 06 '17 at 13:12
  • I figured it out, none of the suggested stuff in the comments. I will add an answer. – Yamen Nassif Nov 06 '17 at 13:19

1 Answers1

0

The problem was the curl command from PHP is not reading the file content, but it was reading the path of the file as a post parameter.

    $request = curl_init('http://10.0.0.106:8089/uploadAndMatch/');
    // send a file
    curl_setopt($request, CURLOPT_POST, true);
    $fileName = explode("/", $file);
    $fileName = end($fileName);
    if (function_exists('curl_file_create')) { // php 5.5+
      $cFile = curl_file_create($file,'text/csv',$fileName );
    } else { //
      $cFile = '@' . realpath('/var/www/html/uploads/ss.csv');
    }
    $post = array('file'=> $cFile);
    curl_setopt($request,CURLOPT_POSTFIELDS,$post);
    curl_setopt($request, CURLOPT_RETURNTRANSFER, true);

and the second problem i solved was that the file in curl_create_file($fileName) was sent to the server as a full path and using curl_file_create($filename, $mimetype = '', $postname = '') fixed it for me.

Yamen Nassif
  • 2,416
  • 2
  • 24
  • 48