How to add an external image to node with a module? I have created a form through my module in which the user(anonymous) can enter an image URL and some title for the image.
The link needs to be, like with remote stream wrapper, shown as an actual image.
So this means, the image should be used from an external sources and not be download on my local server.

$node = new stdClass(); // Create a new node object
$node-&gt;type = "article"; // Or page, or whatever content type you like
node_object_prepare($node);

$node-&gt;title    = filter_xss($_GET['title']);
$node-&gt;language = en; // Or e.g. 'en' if locale is enabled

$file_path = $_GET['link'];

$file = (object) array(
        'uid' =&gt; 1,
        'uri' =&gt; $file_path,
        'filemime' =&gt; file_get_mimetype($file_path),
        'status' =&gt; 1,
);

$node-&gt;field_image[$node-&gt;language][0] = (array)$file;

$node-&gt;uid = 1; // UID of the author of the node; or use $node-&gt;name

node_save($node);


I have this code and it doesn't save the image URL. I'm sending the 'link' and 'title' properties through AJAX.
File field references are made on the fid column in the file_managed table -  you haven't saved the file object so it doesn't have a fid, and thus nothing to relate the field data to.

Just add a call to file_save after you've prepared the file object, but before you add it to the field on the node object:

file_save($file);