How to programmatically create node with image file saved from remote source? I have a remote image URL address saved in $image_url variable and the following code to programmatically create a node:

  $node = new stdClass();
  $node-&gt;uid      = '1';
  $node-&gt;type     = 'article';
  node_object_prepare($node);
  $node-&gt;title    = $title;
  $node-&gt;status   = 1;
  $node-&gt;promote  = 0;
  $node-&gt;sticky   = 0;
  $node-&gt;language = 'ru';
  $node-&gt;body['und'][0]['value']   = $body;
  $node-&gt;body['und'][0]['format']  = 'filtered_html';
  node_save($node);


Calling filefield_sources_save_file($image_url) in a custom module gives:


  Warning: filesize(): stat failed for http://www.remotesite.com/images/2014/11/1414790760.jpg in filefield_sources_save_file() (line 451 of /home/mysite/public_html/sites/all/modules/filefield_sources/filefield_sources.module).
  The specified file 1414790760.jpg could not be uploaded. Only files with the following extensions are allowed: jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp.


Could you please help me to make changes into above peace of code to programmatically create a node with the image in the imagefield saved from a remote source.
Finally have figured out. The following code works fine:

$file = file_save_data(file_get_contents($image_url), file_default_scheme().'://tmp/'.basename($image_url));
$file-&gt;status = 1;
$node-&gt;field_image['und'][0] = (array)$file;

node_save($node);