如何创建一个发送JSON数据和二进制文件的cURL帖子?

I'm trying to create a PHP script which sends some JSON data together with a zipped file to an API (InMobi) but I can't get it to work. I usually just set the header to application/json and use curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); where $data is a PHP array but how do I attach the file on top of that?

The request should look like this:

Content-Type: multipart/form-data; boundary=AYip2bnLbOw0R2vf8lDZ71pon3CDottfPlckt-E Content-Disposition: form-data; name="payload"
Content-Type: application/json; charset=US-ASCII
Content-Transfer-Encoding: 8bit
{
    "campaign": {
        "name": "campaign-with-banner-ad",
        "dailyBudget": "2000.00",
        "startDate": "2012-03-10",
        "endDate": "2012-05-31",
        "action": "create",
        "adGroups": [
            {
                "countryId": 94,
                "name": "First AdGroup",
                "bid": "1.89",
                "landingURL": "http://test.inmobi.com/sampleapp",
                "ctaDetails": {
                    "id": "1"
                },
                "action": "create",
                "ads": [
                    {
                        "type": "banner",
                        "name": "From Root",
                        "filePath": "/",
                        "altText": "3 img add",
                        "action": "create"
                    },
                    {
                        "type": "banner",
                        "name": "From ad1",
                        "filePath": "/ad1/",
                        "action": "create"
                    },
                    {
                        "type": "banner",
                        "name": "From ad2",
                        "filePath": "/ad2/",
                        "action": "create"
                    }
                ]
            }
        ]
    }
}
--AYip2bnLbOw0R2vf8lDZ71pon3CDottfPlckt-E
Content-Disposition: form-data; name="zipFile"; filename="banners.zip" Content-Type: application/octet-stream; charset=ISO-8859-1
Content-Transfer-Encoding: binary

<Contents of ZIP File>

In all of the examples I've seen they put the file in CURLOPT_POSTFIELDS but I don't know how to combine that with json_encode and the above request looks like the file is sent in a separate part of the request.

Thanks!

Split the data into two fields:

$postdata = array(
    'json' => json_encode($whatever),
    'zipfile' => '@/path/to/yourfile.zip'
);

curl_setopt($curl, CURLOPT_POST_FIELDS, $postdata);

Then on the receiving end:

$json = json_decode($_POST['json');
$file = $_FILES['zipfile'];