Creating posts with links from a txt file

I have a script which scrapes articles from the web and saves the url to a .txt file. I've created a custom plugin which on activation, loops through the urls and creates a draft post with the content being an embedded article. I'm using the following code in my plugin

?php
register_activation_hook(__FILE__, 'my_plugin_activate');
function my_plugin_activate(){
    my_plugin_install_site();
}

function my_plugin_install_site(){
    global $user_ID;
    $handle = fopen(listOfURLs.txt, r);
    if ($handle) {
        $count = 0;
        while (($line = fgets($handle)) !== false) {
            // process the line read.
            PostCreator('test', 'post', 'iframe src=' . trim($line) . ' class=alumni-embeded-page/iframe');
        }
        fclose($handle);
    }
    // PostCreator( 'test1', 'post', 'iframe src=[link] class=alumni-embeded-page/iframe' );
    // PostCreator( 'test2', 'post', 'iframe src=[link] class=alumni-embeded-page/iframe' );
}

function PostCreator(
    $name      = 'AUTO POST',
    $type      = 'post',
    $content   = 'DUMMY CONTENT',
    $category  = array(1,2),
    $template  = NULL,
    $author_id = '1',
    $status    = 'draft'
) {
    $post_data = array(
        'post_title'    = wp_strip_all_tags( $name ),
        'post_content'  = $content,
        'post_status'   = $status,
        'post_type'     = $type,
        'post_author'   = $author_id,
        'post_category' = $category,
        'page_template' = $template
    );
    $post_id = wp_insert_post($post_data); 
}

The listOfURLs.txt file is simply a text file in the same directory with a link to an article on every line. I'm only using 5 links to test at the moment. The problem is currently the script does not create any posts when trying to use the loop. The PostCreator() method did work when I coded the method calls manually(as shown by the comments).

I'm also getting the following error when activating the plugin. Not sure it's related as it was still displayed when hard coding the method calls.

The plugin generated 205 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.

Topic wp-insert-post embed plugin-development hooks posts Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.