如何在PHP中使用curl生成api请求

时间:2017-04-11 11:43:34

标签: php json api multipart

我试图将post方法中的图片网址发送到给定的端点api网址,但他们说"问题与您的请求发送图片网址但API期待的事实有关一个multipart / form-data请求,其中包含请求中嵌入的图像数据。"

    "request": {
    "method": "POST",
    "url": "https://api-pro.jivo.com/api/image",
    "httpVersion": "unknown",
    "headers": [
        {
            "name": "Accept",
            "value": "application/json"
        },
        {
            "name": "Referer",
            "value": "https://pro.jivo.com/advert"
        },
        {
            "name": "Origin",
            "value": "https://pro.jivo.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 xxxxxxxx"
        },
        {
            "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"
    }
}

这是我必须使用cURL PHP发送的图像http://mai.com/images/DC/Avanti.jpe

请帮我解决此问题

这是我的代码

$imagedata = array(
        'text' => new CURLFile(
            'http://mai.com/images/DC/Avanti.jpeg', 
            'multipart/form-data'
         ),"image_collection_id"=>186

    );

    $headers2= array('Accept: application/json','Referer: https://pro.tradus.com/advert','Origin: https://pro.jivo.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.jivo.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, CURLOPT_HTTPHEADER, $headers2);
    curl_setopt($ch2, CURLOPT_POSTFIELDS,$imagedata);
    $response2 = curl_exec($ch2);
    $err = curl_error($ch2);
    curl_close($ch2);
    echo $response2;

0 个答案:

没有答案