Jump to content
Sign in to follow this  
blackcell

PHP and cURL - Multiform request with file and text

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?

Share this post


Link to post
Share on other sites

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.

Sign in to follow this  

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