简体   繁体   中英

API request response sending 0 (zero) using curl php

i'm trying to send request in multipart/form-data for image url, but when i'm trying with @ to convert i'm getting 0 as response. Please help me to resolve this issue

$img='http://mai.com/images/DC/Avanti.png';
    $imagedata = array(
        "name"=>'@'.$img,
        "image_collection_id"=>$fid
    );
    $headers2= array('Accept: application/json','Content-Disposition: form-data','Referer: https://pro.avio.com/advert','Origin: https://pro.avio.com','Content-Type: multipart/form-data',"Authorization: Bearer " . $access_token,'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'); 
    $ch2 = curl_init();
    curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch2, CURLOPT_URL, 'https://api-pro.avio.com/api/image');
    curl_setopt($ch2, CURLOPT_POST, true);
    curl_setopt($ch2, CURLOPT_HEADER, true);
    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch2, CURLINFO_HEADER_OUT, true);//
    curl_setopt($ch2, CURLOPT_HTTPHEADER, $headers2);
    curl_setopt($ch2, CURLOPT_POSTFIELDS,$imagedata);
    $response2 = curl_exec($ch2);
    $err = curl_error($ch2);
    $resultStatus = curl_getinfo($ch2, CURLINFO_HTTP_CODE);
    curl_close($ch2);
    echo $resultStatus;

And sample request given by the developer is

"request": {
    "method": "POST",
    "url": "https://api-pro.avio.com/api/image",
    "httpVersion": "unknown",
    "headers": [
        {
            "name": "Accept",
            "value": "application/json"
        },
        {
            "name": "Referer",
            "value": "https://pro.avio.com/advert"
        },
        {
            "name": "Origin",
            "value": "https://pro.avio.com"
        },
        {
            "name": "User-Agent",
            "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
        },
        {
            "name": "Authorization",
            "value": "Bearer xxxxxx"
        },
        {
            "name": "Content-Type",
            "value": "multipart/form-data; boundary=----WebKitFormBoundaryq3A6Lz2bINdfNick"
        }
    ],
    "queryString": [],
    "cookies": [],
    "headersSize": -1,
    "bodySize": 295,
    "postData": {
        "mimeType": "multipart/form-data; boundary=----WebKitFormBoundaryq3A6Lz2bINdfNick",
        "text": "------WebKitFormBoundaryq3A6Lz2bINdfNick\r\nContent-Disposition: form-data; name=\"name\"; filename=\"shippable.png\"\r\nContent-Type: image/png\r\n\r\n\r\n------WebKitFormBoundaryq3A6Lz2bINdfNick\r\nContent-Disposition: form-data; name=\"image_collection_id\"\r\n\r\n175\r\n------WebKitFormBoundaryq3A6Lz2bINdfNick--\r\n"
    }
}

Try using the CURLFile (check here )

Instead of prepending the filename with an @ symbol, you should do something like the following

$img='http://mai.com/images/DC/Avanti.png';
$imagedata = new CURLFile($img);

And you leave the rest as is and call the POST fields as

curl_setopt($ch2, CURLOPT_POSTFIELDS, $imagedata);

You may found more information about the @ operator here . You should specifically check the CURLOPT_SAFE_UPLOAD option.

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