Can't create node programmatically I have this code

&lt;?php
      $node = (object) array(

        'title' =&gt; 'Title',

        'body' =&gt; array(

          LANGUAGE_NONE =&gt; array(

            0 =&gt; array(

              'value' =&gt; 'Body Text',

              'summary' =&gt; 'Summary',

              'format' =&gt; 'filtered_html',
            )
          ),
        ),

        'type' =&gt; 'article',
        'language' =&gt; LANGUAGE_NONE,

        'uid' =&gt; 1,
      );

      node_object_prepare($node);

      $filepath = drupal_realpath('http://ppcdn.500px.org/52335278/cbc55dab399a4bd9caf7a27e9bb3564bc8458f65/3.jpg');

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

      $file = file_copy($file, 'public://');

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

      node_save((object) $node);

      $path['alias'] = 'our-great-dsk-node';
      $path['source'] = 'node/' . $node-&gt;nid;
      $path['language'] = LANGUAGE_NONE;
      path_save($path);

      cache_clear_all();
    ?&gt;


But can't understand where to use this code to create nodes programmatically in drupal 7 or how to use this code? To test i saved this as custom-posts.php in drupal root directory to see if this code works or not, but it gives a lot errors, so what to include?
To bootstrap, try adding the following lines of code to the top of your custom-posts.php file:

$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
define('DRUPAL_ROOT', '/var/www/drupal7');
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);


Don't forget to update the path to your Drupal root folder.