How can I set a node reference programmatically? I'm trying to write a custom PHP action in Rules module.  Each time after creation of a certain type of content, it should create an additional node with a reference to the created node.( Field by Node reference module )

I've tried to pass the values of nid and/or title of the main node to this field using tokens and it didn't work.

If the field is a text field it passes the variables correctly.

What value should be passed to the node reference field to set it in code?
First, I"ll answer your question, then suggest another way.

Assuming that this is a monolingual site, use

$additional_node-&gt;field_created_node[LANGUAGE_NONE][0]['nid'] = $created_node-&gt;nid;

However, Rules already has actions that can change values on a node, and when you create a node using Rules, it creates a new variable containing the created node. If I were you, I'd just add actions that use that data and not write custom PHP code to do it. I just created one to assign populate a user reference field on a created node (within the same rule) the other day, and it worked fine.