简体   繁体   English

API请求响应使用curl php发送0(零)

[英]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. 我正在尝试在multipart / form-data中发送图像URL的请求,但是当我尝试使用@进行转换时,我得到0作为响应。 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 ) 尝试使用CURLFile在此处检查)

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 您将其余部分保持不变,并按以下方式调用POST字段:

curl_setopt($ch2, CURLOPT_POSTFIELDS, $imagedata);

You may found more information about the @ operator here . 您可以在此处找到有关@运算符的更多信息。 You should specifically check the CURLOPT_SAFE_UPLOAD option. 您应该专门检查CURLOPT_SAFE_UPLOAD选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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