{"id":2332,"date":"2014-10-12T22:46:24","date_gmt":"2014-10-12T19:46:24","guid":{"rendered":"http:\/\/9v.lt\/blog\/?p=2332"},"modified":"2022-01-19T08:34:38","modified_gmt":"2022-01-19T06:34:38","slug":"sending-multipart-post-requests-php","status":"publish","type":"post","link":"https:\/\/9v.lt\/blog\/sending-multipart-post-requests-php\/","title":{"rendered":"Sending multipart POST requests with PHP"},"content":{"rendered":"<p>A long time ago I wrote about the same thing, but with more text and a C++ example, <a href=\"http:\/\/9v.lt\/blog\/sending-multipart-post-requests-c\/\" title=\"Sending multipart POST requests with C++\" target=\"_blank\" rel=\"noopener\">you can find it here<\/a>. This time I made the same thing but in PHP and using cURL, because it was for a project I was working on. Basically I had a very simple task that needed to be a bit automated. Whole website is also basic, just simple authentication and then a simple file upload. In this project generate a lot of data (around 5mb) and then POST all of that to the file upload script.<br \/>\n<!--more--><\/p>\n<p>For the uploading part I took the snippet from <a href=\"http:\/\/vedovini.net\/2009\/08\/posting-multipart-form-data-using-php\/\" target=\"_blank\" rel=\"noopener\">this website<\/a> and changed it up. The original code was missing few <code>eol<\/code>&#8216;s. This function is tuned for my needs, you will most likely have to change it up to fit your environment.<br \/>\nHere is the code I used:<\/p>\n<pre lang=\"php\">\r\npublic function uploadData($uploadData) {\r\n    \/\/ echo '<pre>'; print_r($uploadData); die();\r\n    $username = 'fancyuser';\r\n    $password = 'simplepasswd';\r\n    $loginUrl = 'http:\/\/some.website.com\/login.php';\r\n    $uploadUrl = 'http:\/\/some.website.com\/upload.php';\r\n    \r\n    \/\/ here we log in and get a cookie\r\n    $ch = curl_init();\r\n    curl_setopt($ch, CURLOPT_URL, $loginUrl);\r\n    curl_setopt($ch, CURLOPT_POST, true);\r\n    \/\/ what to send to authenticate, use \"Live HTTP Headers\" for firefox or something to know what to send\r\n    curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.$username.'&password='.$password);\r\n    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); \/\/ make sure the dir where this script is located at is writeable\r\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\r\n    $store = curl_exec($ch); \/\/ redirection url\r\n    if (strpos($store, 'url=upload.php') !== false) { \/\/ yeah log in was successful\r\n        \/\/ curl_setopt($ch, CURLOPT_URL, $uploadUrl);\r\n        \/\/ $content = curl_exec($ch);\r\n        \/\/ file_put_contents('ish.txt', $store); die();\r\n        \/\/ echo 'logged in<br \/>';\r\n        \r\n        \/\/ construct the upload body\r\n        $eol = \"\\r\\n\";\r\n        $data = '';\r\n        $mime_boundary = md5(time());\r\n        $data .= '--'.$mime_boundary.$eol;\r\n        $data .= 'Content-Disposition: form-data; name=\"someformname\"; filename=\"somefile.ext\"'.$eol;\r\n        $data .= 'Content-Type: application\/octet-stream'.$eol.$eol;\r\n        $data .= $uploadData.$eol;\r\n        $data .= '--'.$mime_boundary.$eol;\r\n        $data .= 'Content-Disposition: form-data; name=\"_action\"'.$eol.$eol;\r\n        $data .= 'upload'.$eol;\r\n        $data .= '--'.$mime_boundary.'--'.$eol.$eol;\r\n        \/\/ start to upload everything\r\n        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart\/form-data; boundary='.$mime_boundary));\r\n        curl_setopt($ch, CURLOPT_URL, $uploadUrl);\r\n        curl_setopt($ch, CURLOPT_POST, true);\r\n        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);\r\n        $response = curl_exec($ch);\r\n        \/\/ echo $response; die();\r\n        return $this->parseResponse($response);\r\n    } else {\r\n        return array('uploadInfo' => 'Failed to login', 'fileInfo' => 'Bad username or password');\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A long time ago I wrote about the same thing, but with more text and<\/p>\n","protected":false},"author":2,"featured_media":2199,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,750],"tags":[918,879,893],"class_list":["post-2332","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-projects","category-software-projects","tag-file-upload","tag-multipart","tag-php"],"_links":{"self":[{"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/posts\/2332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/comments?post=2332"}],"version-history":[{"count":0,"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/posts\/2332\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/media\/2199"}],"wp:attachment":[{"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/media?parent=2332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/categories?post=2332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/9v.lt\/blog\/wp-json\/wp\/v2\/tags?post=2332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}