使用-F选项cURL命令将PUT转换为cURL PHP

时间:2018-03-28 03:56:48

标签: php curl put

我想将一个正常工作的cURL命令行转换为PHP代码。 这是我的命令行:

curl -k -v -x api.test.sandbox.mobile.de:8080 -u xxx:yyy -H "Content-Type: multipart/form-data" -H "Accept: application/vnd.de.mobile.api+json" -F "image1=@1000_1.JPG;type=image/jpg" -X PUT https://services.mobile.de/seller-api/sellers/1086/ads/943627/images

这很正常,我试图将cURL命令行转换为这个cURL PHP代码:

 $ch = curl_init();

    $proxy = PROXY_MOBILE_DE;
    $proxy_port = PROXY_PORT_MOBILE_DE;
    $loginpassw = LOGINPASSWD_MOBILE_DE;

    $post_data = array(
            "image1" => '@1000_1.JPG',
            "type" => 'image/jpg'
    );

    $url='https://services.mobile.de/seller-api/sellers/'.$sellerId.'/ads/'.$vehicleAdId.'/images';

    $headers = array();
    $headers[] = "Content-Type: multipart/form-data";
    $headers[] = "Accept: application/vnd.de.mobile.api+json";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
    curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));

    curl_setopt($ch, CURLOPT_USERPWD, $loginpassw);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

    $data = curl_exec($ch);
    if (curl_errno($ch)) {
        $info = curl_getinfo($ch);
        echo "Error: ".$info['http_code']." // ".$info['url'];
    }

    if ($data=='')
        echo 'Success ';
    else
        echo 'Failure'.$data;
    echo "<br/>";

    curl_close($ch);

但我收到类似&#34的错误;请求被拒绝,因为没有找到多部分边界&#34;。我知道哪里错了?

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

$post_data = array(
    "image1" => '@1000_1.JPG',
    "type" => 'image/jpg'
);
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://services.mobile.de/seller-api/sellers/1086/ads/943627/images");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_PROXY, "api.test.sandbox.mobile.de:8080");
curl_setopt($ch, CURLOPT_USERPWD, "xxx:yyy");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));

$headers = array();
$headers[] = "Content-Type: multipart/form-data";
$headers[] = "Accept: application/vnd.de.mobile.api+json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);