Tuesday, July 9, 2013

Php Curl Multipart form posting

Let's say you want to post some data to a form using curl:

$url = 'http://www.example.com/';
$data = array('foo' => '1', 'bar' => '2');

Ordinarily you create the post body using http_build_query(). But let's say the form is expecting the form data to be multipart encoded. Now we've got a challenge. First let's create a function to do the multipart encoding:

function multipart_build_query($fields, $boundary){
  $retval = '';
  foreach($fields as $key => $value){
    $retval .= "--$boundary\nContent-Disposition: form-data; name=\"$key\"\n\n$value\n";
  }
  $retval .= "--$boundary--";
  return $retval;
}

The boundary is a string that separates the fields. It can be any string you want but you should choose something long enough that it won't randomly show up in your data.

$boundary = '--myboundary-xxx';
$body = multipart_build_query($data, $boundary);


Now make your curl post, but remember to set the content type:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data; boundary=$boundary"));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$response = curl_exec($ch);

Of course, you could have just used PGBrowser to submit the form, and it will automatically detect when it needs to use multipart encoding. But that would be too easy.

40 comments:

  1. I had to modify it a little. Specifically the forms wanted \r\n not just \n

    function multipart_build_query($fields, $boundary){
    $retval = '';
    foreach($fields as $key => $value){
    $retval .= "--$boundary\r\nContent-Disposition: form-data; name=\"$key\"\r\n\r\n$value\r\n";
    }
    $retval .= "--$boundary--";
    return $retval;
    }

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi,

    Can any one say how to convert pdf file to byte array and post the array using curl.

    Thank you

    ReplyDelete
  4. submit Track not work with http://www.vnpost.vn/

    ReplyDelete
  5. Very well written. Must be a professional scrapper. Helps me a lot dude.

    ReplyDelete
  6. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
    redmi service center in chennai
    mi service center in chennai

    ReplyDelete
  7. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete
  8. I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!

    business analytics course

    data analytics courses

    data science interview questions

    data science course in mumbai

    For more info :

    ExcelR - Data Science, Data Analytics, Business Analytics Course Training in Mumbai

    304, 3rd Floor, Pratibha Building. Three Petrol pump, Opposite Manas Tower, LBS Rd, Pakhdi, Thane West, Thane, Maharashtra 400602
    18002122120

    ReplyDelete
  9. I have read your blog its very attractive and impressive. I like it your blog. Data-Analytics course

    ReplyDelete

  10. Nice blog,I understood the topic very clearly,And want to study more like this.
    Data Scientist Course

    ReplyDelete

  11. Nice blog,I understood the topic very clearly,And want to study more like this.
    Data Scientist Course

    ReplyDelete
  12. It’s very informative and you are obviously very knowledgeable in this area. You have opened my eyes to varying views on this topic with interesting and solid content.
    data analytics courses

    ReplyDelete
  13. Actually I read it yesterday but I had some thoughts about it and today I wanted to read it again because it is very well written.
    Data Analyst Course

    ReplyDelete
  14. very well explained. I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    Logistic Regression explained
    Correlation vs Covariance
    Simple Linear Regression
    data science interview questions
    KNN Algorithm

    ReplyDelete


  15. Impressive blog to be honest definitely this post will inspire many more upcoming aspirants. Eventually, this makes the participants to experience and innovate themselves through knowledge wise by visiting this kind of a blog. Once again excellent job keep inspiring with your cool stuff.

    Data Science Training in Bhilai

    ReplyDelete
  16. You really make it look so natural with your exhibition however I see this issue as really something which I figure I could never understand. It appears to be excessively entangled and incredibly expansive for me.
    data analytics courses in hyderabad

    ReplyDelete
  17. This post is so interactive and informative.keep update more information...
    Salesforce Training in Tambaram
    Salesforce Training in Anna Nagar

    ReplyDelete
  18. I don t have the time at the moment to fully read your site but I have bookmarked it and also add your RSS feeds. I will be back in a day or two. thanks for a great site.data science training in bhopal

    ReplyDelete
  19. This is a great post and needs to be appreciated a lot thank you for sharing this information.

    Data science courses in Ghana

    ReplyDelete
  20. The blog on "PHP Curl Multipart Form Posting" provides a clear and concise guide to handling multipart form data in PHP using cURL. It outlines the challenge of posting data when a form expects multipart encoding, and the custom function multipart_build_query to create the multipart post body is well-explained. The explanation of setting a boundary and properly configuring the cURL options is straightforward and easy to follow. Overall, it’s a useful tutorial for anyone working with multipart form data in PHP. However, it might be worth mentioning that there are libraries like PGBrowser that can automate this process, making multipart form posting even simpler.
    data analytics courses in dubai

    ReplyDelete
  21. Using PHP cURL for multipart form posting is a powerful way to send data, especially when dealing with file uploads. It simplifies the process of interacting with forms programmatically. Looking forward to more insights on how to implement this effectively! Data Science Courses In Malviya Nagar ​

    ReplyDelete
  22. Great content! The demand for data scientists is on the rise. For those interested in entering this field, the Data science courses in Faridabad provide a solid foundation. Don’t miss the opportunity!

    ReplyDelete
  23. Fantastic tutorial on PHP cURL multipart form posting! Your clear explanations make it easy to grasp complex concepts. This is a valuable resource for developers looking to enhance their skills. Keep up the great work!
    Data Science Courses in Singapore





    ReplyDelete
  24. "Thanks for the clear guide on using PHP cURL for multipart form posting! Your step-by-step instructions make it easy to follow along, and the examples are very helpful. I appreciate the additional tips you provided for handling file uploads. This is a fantastic resource for anyone working with forms in PHP
    Data science courses in Bhutan

    ReplyDelete
  25. Excellent post on PHP cURL multipart form posting! You made a complex topic feel much more approachable. The examples were super helpful, and I appreciate how clear your writing is. Great content.
    Online Data Science Course







    ReplyDelete
  26. A great article with a detailed explanation. Thanks for sharing.
    Data Science Courses in Hauz Khas

    ReplyDelete
  27. The blog on "PHP Curl Multipart Form Posting" offers a clear guide on handling multipart form data in PHP using cURL. It explains the process well, from creating a multipart post body to configuring cURL options. Mentioning tools like PGBrowser could further simplify the process for users.

    ReplyDelete
  28. This is a great post on using PHP cURL for multipart form posting! You’ve explained the process in a clear and concise way, making it easy to understand how to handle file uploads and form submissions with cURL. The examples provided are really practical for anyone looking to work with APIs or handle form data in PHP. Thanks for sharing this useful tutorial.
    Data science course in Gurgaon

    ReplyDelete
  29. I liked how thorough your post is. Your detailed approach to Php Curl Multipart form posting makes it easy to understand. Thanks for the helpful article.
    Data Science Courses in China


    ReplyDelete
  30. Thank you for sharing your knowledge in this article. It was both informative and engaging!
    GST Course

    ReplyDelete
  31. Your guide on PHP cURL multipart form posting is excellent! You’ve simplified a complex topic and made it accessible for everyone. Thanks for this great resource!
    Data science courses in Bangalore

    ReplyDelete