How to get a remote file and set it as a value for a file field using entity metadata wrappers? I am trying to get a remote pdf file from a web service and save it as a field's value using entity_metadata_wrapper, but somehow I couldn't get it working.
I can successfully retrieve the file using system_retrieve_file and save it locally. However, I couldn't set the file field's value to this newly retrieved file.
Here is my code (simplified):
// Retrieve the file from the web service.
// This works, I can see and open the retrieved file.
$file = system_retrieve_file($file_url, 'public://path/', TRUE);
$file = (array) $file;
var_dump($file);

$ewrapper = entity_metadata_wrapper('node', $nid)

// Set this file as a value for the node's file field.
// I've tried following lines individually and they both failed.
$ewrapper-&gt;field_pdffile-&gt;set($file);
$ewrapper-&gt;field_pdffile-&gt;set(array($file));

Both methods I've tried raised the following error:

EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. in EntityMetadataWrapper-&gt;set() (line 122 of /drupal/sites/all/modules/entity/includes/entity.wrapper.inc).

I'm setting in the same code block other fields as well and they are set and saved without any error if I skip the file field.
For example, the following lines are working in the same code block for other fields:
// I'm getting the text from the web service
$ewrapper-&gt;field_text-&gt;set($some_text);
// I'm getting a term from web service
// Creating it if it doesn't already exist, and saving the 'term id' like:
$ewrapper-&gt;field_term-&gt;set($tid);
// A few more various fields are successfully set like that...
// Except that file field, which returns the above exception:
$ewrapper-&gt;field_pdffile-&gt;set($file);

What could it be, that I'm doing wrong?
UPDATE:
I've added a var_dump($file) and I'm getting the following output, which seems alright to me:
array(17) {
  'fid' =&gt; string(5) &quot;13037&quot;
  'uid' =&gt; string(1) &quot;1&quot;
  'filename' =&gt; string(31) &quot;sample.pdf&quot;
  'uri' =&gt; string(54) &quot;public://path/sample.pdf&quot;
  'filemime' =&gt; string(15) &quot;application/pdf&quot;
  'filesize' =&gt; string(6) &quot;129804&quot;
  'status' =&gt; string(1) &quot;1&quot;
  'timestamp' =&gt; string(10) &quot;1409924961&quot;
  'type' =&gt; string(8) &quot;document&quot;
  'uuid' =&gt; string(36) &quot;a06f6bf7-61e1-48fd-bfc7-8bff6e72705f&quot;
  'metadata' =&gt; array(0) {}
  'alt' =&gt; string(0) &quot;&quot;
  'title' =&gt; string(0) &quot;&quot;
}
Since @xurshid29 never post the correct answer here, I am posting it cause I find it "hard" to retrieve the comments:

(from @ЕлинЙ post)

// Retrieve file from web service (this works, I can see and open the retrieved file)
$file = system_retrieve_file($file_url, 'public://path/', TRUE);
$file = (array) $file;
$file['display'] = 1;
$file['description'] = '';

$ewrapper = entity_metadata_wrapper('node', $nid)

// Set this file as a value for the node's file field.
$ewrapper-&gt;field_pdffile-&gt;set($file);


More info on system_retrieve_file

And thanks to both of you for your work!