I have reviewed the tutorials and snippets on the Drupal site (http://drupal.org/node/87711) and I have read several discussions about the topic. Yet I'm still having 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?

$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,
'taxonomy[3]'=>'2',
  '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);

Comments

digger3d’s picture

Hello. I am having trouble with an image upload. In the ubercart form it is a JS(imagecache) controlled image field, which I don't know how to fill from outside drupal using CURL...
The idea is to bulk add products with images from my local(not drupal) database of products to the remote drupal site.

The node adds, with all the custom fields I've created, but there is no image uploading. It is just no image. As I think it is handled by filefield/imagecache.
But how to put the image in it?
I have tried files[field_image_cache_0] and field_image_cache[0][fid] but nothing happens. Node adds with no image in it.
I think it works like this:
JS uses filefield to upload an image and then uses imagecache to put it in the form, so by the time of submitting the form the image is already uploaded and a reference is added to Drupal database.

Code is here http://drupal.org/node/89710#comment-3380216

Please help me.