[development] Add image node with PHP Curl?

James Tunick jt722 at nyu.edu
Tue Jul 17 16:18:25 UTC 2007

I have read a lot about about the topic. But I'm still a newbie and I'm having serious trouble.

After using PHP/Curl to login, I move to the node/add/image page and get the token no problem. But I'm having trouble posting a new image node. I can get everything working when posting a new page node with Curl. It's just posting a new image node that's giving me trouble.

Here is the code that should add the new image node. Curl fills out most of the image form fields such as title, body etc. All fields are populated by Curl *EXCEPT* the image file upload. And this seems to be what's stopping curl from creating the new image node. What am I doing wrong? How can I get Curl to fill in the file upload field? Any suggestions much appreciated!

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/?q=node/add/image/");
curl_setopt($ch, CURLOPT_POST, 1);

$postarray = array(
'files[image]' =>'@$file',
'title' => $file,
'body' => 'xxxx',
'status' => '1',
'revision' => '1',
'comment' => '2',
'name' => 'mov',
'moderate' => '0',
'form_id' => 'image_node_form',
'form_token' => $token,
'op' => 'Submit'


curl_setopt($ch, CURLOPT_POSTFIELDS, $postarray);

$buf2 = curl_exec ($ch);

curl_close ($ch);

I have reviewed the tutorials and snippets on the Drupal site (http://drupal.org/node/87711) and on other sites (http://www.voidberg.org/blog/using_php_and_curl_to_automate_drupal_tasks_such_as_node_adding_or_user_adding.html)

More information about the development mailing list