Create a field collection field programmatically Is there a way to create a field collection programmatically? I saw this code: 

$field_collection_item = entity_create('field_collection_item', array('field_name' =&gt;'field_text')); // Create new field collection item.
$field_collection_item-&gt;setHostEntity('node', $node); // This is required, there shuold be a host entity, this case I used a node.
$field_collection_item-&gt;field_text[LANGUAGE_NONE][0]['value'] = 'Test Value';
$field_collection_item-&gt;save(); // Save the field collection item.


But when I run this code I get this error: 


  Exception: Invalid field name given: field_text is not a Field
  Collection field.


I can't find a proper solution for this.
Have you tried using entity metadata wrappers? They come with the Entity API module and allow you to do this (copied from linked documentation):

&lt;?php
    // Populate the fields.
    $ewrapper = entity_metadata_wrapper('node', $node);
    $ewrapper-&gt;field_lead_contact_name-&gt;set($contact_name);
    $ewrapper-&gt;field_lead_contact_phone-&gt;set($contact_phone);
    $ewrapper-&gt;field_lead_contact_email-&gt;set($contact_email);  

    // Create the collection entity and set it's "host".
    $collection = entity_create('field_collection_item', array('field_name' =&gt; 'field_facilities_requested'));
    $collection-&gt;setHostEntity('node', $node);  

    // Now define the collection parameters.
    $cwrapper = entity_metadata_wrapper('field_collection_item', $collection);
    $cwrapper-&gt;field_facility-&gt;set(intval($offset));
    $cwrapper-&gt;save();  

    // Save.
    $ewrapper-&gt;save();
?&gt;


In this example a node and the attached fieldset are created at the same time, so it might just work with only below code. Perhaps it is required to save the node afterwards, I have not tested it.

&lt;?php
    // Create the collection entity and set it's "host".
    $collection = entity_create('field_collection_item', array('field_name' =&gt; 'field_facilities_requested'));
    $collection-&gt;setHostEntity('node', $node);  

    // Now define the collection parameters.
    $cwrapper = entity_metadata_wrapper('field_collection_item', $collection);
    $cwrapper-&gt;field_facility-&gt;set(intval($offset));
    $cwrapper-&gt;save();
?&gt;


Update:

At the bottom of the Entity Metadata Wrapper documentation (before the comments) there are some lines about debugging and finding available parameters. That also might be of help while attempting to find the right property.