Jump to content

PHP and cURL - Multiform request with file and text


blackcell

Recommended Posts

I need to send a cURL request containing a file to upload as well as a JSON string. I can get a properly formatted request when sending the JSON, but the file is throwing it off.

    $curl_file_object = '@'.$tmp_name
    $postData= array(
        'JSON'=> json_encode($jsonParams),
        $reference => $curl_file_object,
    );

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $target);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data;"));
    curl_setopt($curl, CURLOPT_POST, true); // enable posting
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); // post data
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // if any redirection after upload

    $response = curl_exec($curl);
    $info = curl_getinfo($curl);
    curl_close($curl);
    debug($info);

Am I the first one needing to send file binary data along with a json string to a server at the same time?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.