通过curl发送图像失败


sending images via curl fails

curl无法将照片发送到远程API。

远程服务器在现场接收直接提交时工作正常。文件上传文件名为photos[] (api运行laravel 5,禁用csrf)。

首先我这样做:

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 send 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

问题是所有的字段都被发送和保存,但从来没有图像。我一定是发错了方向!

帮忙吗?

我在这个问题之后运行了1天才发现问题的根源!

我在博客文章中详细描述了我的解决方案:http://labs.weare.de.com/php-curl-multiple-file-upload-nightmare/

特别感谢@bishop为我们指明了正确的方向。

1. curl_setopt( $ch, CURLOPT_POSTFIELDS, $dataArray );
2. $car[ "photos[ $k ]" ]

所以,简单地说,问题是多维数组,我试图使用curl。它不接受它。另一个问题是当你使用http_query_build curl停止发送文件并给出正确的内容类型。

所以我使用php hack:)