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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.