0

Cannot get curl to send photos to remote API.

Remote server works fine when receiving direct submit onsite. File upload filed name is photos[] (api runs laravel 5, with csrf disabled).

First I do this:

foreach ( $carRaw[ 'imgs' ] as $k => $v) {

    $tmpFile = tempnam( '/tmp/curlfiles', 'autoge-' );

    file_put_contents(

        $tmpFile,

        file_get_contents(

            'http://' . $v[ 'ipt' ] . '/im/im-' . $v[ 'ikey' ]

        )
    );

    $car[ 'photos' ][ $k ] = new \CURLFile( $tmpFile, 'image/png', $v[ 'ikey' ] );

}

After my sending method:

    $ch = curl_init();

    curl_setopt( $ch, CURLOPT_URL, $addCarUrl );
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $car ) );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $ch, CURLOPT_COOKIEFILE, $cookiePath );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_VERBOSE, false );

    $response = curl_exec( $ch );

    curl_close( $ch );

    $this->info( $response );

    return $response; // 1 or 0

Problem is that all the fields are sent and saved, but never images. I must be sending them in a wrong way!

Any help?

Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
  • anything in your logs ? – Pogrindis Aug 04 '15 at 14:23
  • @Pogrindis nada, it fails silently :S it actually doesn't fail since everything else is sent and saved. –  Aug 04 '15 at 14:25
  • just out of interest why have you got a `"\"` prior to `CURLFile` ? – Pogrindis Aug 04 '15 at 14:28
  • http://stackoverflow.com/questions/4223977/send-file-via-curl-from-form-post-in-php this might help – Aleksandar Vasić Aug 04 '15 at 14:28
  • @Pogrindis this is cli program, inside laravel 5. Without \ it fails to grab method from global namespace. –  Aug 04 '15 at 14:30
  • `http_build_query` seems incorrect to me. Any better if you just pass `$car`? – bishop Aug 04 '15 at 15:17
  • @bishop POSTFIELDS expects string –  Aug 04 '15 at 15:28
  • No, both [string and array are supported](http://stackoverflow.com/questions/5224790): *"This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value."* – bishop Aug 04 '15 at 15:46
  • @bishop problem is multidimensional array which file sending requires. it is not supported. –  Aug 04 '15 at 16:16
  • MIght be that CURL needs the absolute path rather than relative path tp the dir – Mihai Aug 04 '15 at 19:53
  • @Mihai nope, I fixed issue. –  Aug 04 '15 at 22:31
  • @bishop you were very close! thanks for pointing me to right direction. –  Aug 04 '15 at 22:31

1 Answers1

1

I discovered source of the problem after 1 day of running after this problem!

I described my solution in detail in the blog post: http://labs.weare.de.com/php-curl-multiple-file-upload-nightmare/

Special thanks to @bishop for the correct direction.

1. curl_setopt( $ch, CURLOPT_POSTFIELDS, $dataArray );

2. $car[ "photos[ $k ]" ]

So the problem in nutshell is multidimensional array I was trying to use with curl. It doesn't accept it. Another problem is when you use http_query_build curl stops sending files and giving correct content type.

So I used php hack :)

  • Glad you got it solved. Read your post, I'm sad that you had to hack around it. What about using `http_build_query` but changing the header as required for files: `curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($car)); ($c, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));` – bishop Aug 05 '15 at 13:25
  • 1
    You might also need to compress arbitrary multiple dimensions into just two, as [described in this SO answer](http://stackoverflow.com/a/8224117/2908724). – bishop Aug 05 '15 at 13:28
  • @bishop yes I tried that, problem is that curl gets confused. What I noticed that in case of my hack there is correct content type and boundary, but when I set content type manually boundary part is missing. –  Aug 05 '15 at 13:35