Flag a node programatically I am trying to get flag "purchase" and flag it for an user while completing an order in Rules (Commerce). This is how it goes in a PHP code:

$flag = flag_get_flag('purchase');
$nid = $_SERVER ['REQUEST_URI'];
$nid = explode("node/", $nid);
$node = node_load($nid[1]);
global $user;
$flag-&gt;flag('purchase', $node-&gt;nid, user_load($user-&gt;uid), TRUE);


Does anybody see any inaccuracy in what I've done? I just don't know why it is not working.

This is the error I am getting (actually it says nothing to me):


  Notice: Undefined offset: 1 in eval() (line 4 of C:\xampp\htdocs\mysite\sites\all\modules\rules\modules\php.eval.inc(146) : eval()'d code).
  Notice: Trying to get property of non-object in eval() (line 6 of C:\xampp\htdocs\mysite\sites\all\modules\rules\modules\php.eval.inc(146) : eval()'d code).
  Notice: Trying to get property of non-object in flag_entity->applies_to_entity() (line 130 of C:\xampp\htdocs\mysite\sites\all\modules\flag\includes\flag\flag_entity.inc).
Try below code... You can read node arguments using arg function in Drupal...

global $user;
$flag = flag_get_flag('purchase');
$node = node_load(arg(1));
$flag-&gt;flag('purchase', $node-&gt;nid, user_load($user-&gt;uid), TRUE);