简体   繁体   中英

sending images via curl fails

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?

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM