help with curl multipart from post
Posted: Sat Mar 12, 2011 2:18 am
hello
i m trying to build a script that can post articles to my drupal based website on automation .but the porblem is the article postng form is multipart .and till now i havent figured out how to build header and postdata for multipart forms.
the header for the drupal form is like :
till now i came up with this script which obviously isnt correct
can somebody please guide me how to deal with multipart. i looked google but i am not able to find out any good example.
First i m not able to figure out how to frame the boundry
then i dont understand how to build the postdata.
If only i can find any good example about how to deal with this kind of forms ...
i m trying to build a script that can post articles to my drupal based website on automation .but the porblem is the article postng form is multipart .and till now i havent figured out how to build header and postdata for multipart forms.
the header for the drupal form is like :
Code: Select all
http://mysite.com/drupal/?q=node%2Fadd%2Farticle&
POST /drupal/?q=node%2Fadd%2Farticle& HTTP/1.1
Host: mysite.com
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:2.0b12) Gecko/20100101 Firefox/4.0b12
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://mysite.com/?q=node%2Fadd%2Farticle&
Cookie: Drupal.visitor.name=abcde; SESS0933c7f6c3b533f5a8c8b5e0739b8056=K6NQKbD8rItUPfuXxGpajYNGi5sh3A6IiSjYdDrIWLo; has_js=1
Content-Type: multipart/form-data; boundary=---------------------------7469163454380
Content-Length: 2836
-----------------------------7469163454380
Content-Disposition: form-data; name="title"
hello there
-----------------------------7469163454380
Content-Disposition: form-data; name="field_tags[und]"
hiiii
-----------------------------7469163454380
Content-Disposition: form-data; name="body[und][0][summary]"
-----------------------------7469163454380
Content-Disposition: form-data; name="body[und][0][value]"
hello everybody
-----------------------------7469163454380
Content-Disposition: form-data; name="body[und][0][format]"
filtered_html
-----------------------------7469163454380
Content-Disposition: form-data; name="files[field_image_und_0]"; filename=""
Content-Type: application/octet-stream
-----------------------------7469163454380
Content-Disposition: form-data; name="field_image[und][0][fid]"
0
-----------------------------7469163454380
Content-Disposition: form-data; name="field_image[und][0][display]"
1
-----------------------------7469163454380
Content-Disposition: form-data; name="changed"
-----------------------------7469163454380
Content-Disposition: form-data; name="form_build_id"
form-X6Kr86bnitVTegkkkTJ01ShYKnMTn2XBVH75BVIIIsA
-----------------------------7469163454380
Content-Disposition: form-data; name="form_token"
cUzbUH440gzTRSxjTj4n9I-omAQ2dByCl7DoVeMvmRE
-----------------------------7469163454380
Content-Disposition: form-data; name="form_id"
article_node_form
-----------------------------7469163454380
Content-Disposition: form-data; name="menu[link_title]"
-----------------------------7469163454380
Content-Disposition: form-data; name="menu[description]"
-----------------------------7469163454380
Content-Disposition: form-data; name="menu[parent]"
main-menu:0
-----------------------------7469163454380
Content-Disposition: form-data; name="menu[weight]"
0
-----------------------------7469163454380
Content-Disposition: form-data; name="log"
-----------------------------7469163454380
Content-Disposition: form-data; name="path[alias]"
-----------------------------7469163454380
Content-Disposition: form-data; name="comment"
2
-----------------------------7469163454380
Content-Disposition: form-data; name="name"
admin
-----------------------------7469163454380
Content-Disposition: form-data; name="date"
-----------------------------7469163454380
Content-Disposition: form-data; name="status"
1
-----------------------------7469163454380
Content-Disposition: form-data; name="promote"
1
-----------------------------7469163454380
Content-Disposition: form-data; name="additional_settings__active_tab"
edit-menu
-----------------------------7469163454380
Content-Disposition: form-data; name="op"
Save
-----------------------------7469163454380--Code: Select all
<?php
$boundary = '-----------------------------7665267813202';
$header = array (
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: en-us,en;q=0.5',
'Accept-Encoding: gzip, deflate',
'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Keep-Alive: 115',
'Connection: keep-alive',
'Referer: http://mysite.com/?q=node%2Fadd%2Farticle&',
'Content-Type: multipart/form-data; boundary=---------------------------7469163454380'
);
$payload = array
(
'Content-Disposition: form-data; name="title"' => "hellooo here",
'Content-Disposition: form-data; name="field_tags[und]"' => "test",
'Content-Disposition: form-data; name="body[und][0][summary]"' => "",
'Content-Disposition: form-data; name="body[und][0][value]"' => "hello everybody",
'Content-Disposition: form-data; name="body[und][0][format]"' => "filtered_html",
'Content-Disposition: form-data; name="files[field_image_und_0]"; filename=""\nContent-Type: application/octet-stream' => "",
'Content-Disposition: form-data; name="field_image[und][0][fid]"' => "0",
'Content-Disposition: form-data; name="field_image[und][0][display]"' => "1",
'Content-Disposition: form-data; name="changed"' => "",
'Content-Disposition: form-data; name="form_build_id"' => "form-X6Kr86bnitVTegkkkTJ01ShYKnMTn2XBVH75BVIIIsA",
'Content-Disposition: form-data; name="form_token"' => "cUzbUH440gzTRSxjTj4n9I-omAQ2dByCl7DoVeMvmRE",
'Content-Disposition: form-data; name="form_id"' => "article_node_form",
'Content-Disposition: form-data; name="menu[link_title]"' => "",
'Content-Disposition: form-data; name="menu[description]"' => "",
'Content-Disposition: form-data; name="menu[parent]"' => "main-menu:0",
'Content-Disposition: form-data; name="menu[weight]' => "0",
'Content-Disposition: form-data; name="log"' => "",
'Content-Disposition: form-data; name="path[alias]"' => "",
'Content-Disposition: form-data; name="comment"' => "2",
'Content-Disposition: form-data; name="name"' => "admin",
'Content-Disposition: form-data; name="date"' => "",
'Content-Disposition: form-data; name="status"' => "1",
'Content-Disposition: form-data; name="promote"' => "1",
'Content-Disposition: form-data; name="additional_settings__active_tab"' => "edit-menu",
'Content-Disposition: form-data; name="op"' => "Save"
);
//$output .= "$boundary\r\n";
//foreach ($payload as $key => $value){
// $output .= $value."$boundary\r\n";
// }
echo " <BR><BR><BR><BR>Payload is:<BR>$output";
$posturl="";
$ch = curl_init($posturl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "Content-Type: multipart/form-data; boundary=--$srand");
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_POSTFIELDS ,$postdata);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,0);
curl_setopt($ch, CURLOPT_COOKIE, $mycookies);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1); // RETURN THE CONTENTS OF THE CALL
$return = curl_exec($ch);
curl_close($ch);
?>First i m not able to figure out how to frame the boundry
then i dont understand how to build the postdata.
If only i can find any good example about how to deal with this kind of forms ...