PHP to get random line from text file for video embed

This is a section of my code that goes and gets videos from any video site. What I’m having issues with is having it post a random title, that are all in a text file called titles.txt. What I’m wanting to accomplish is have my script go pull the video embed code, the duration, the tags, etc. Which all works fine until I get to the title part where it pulls locally from the server from the text file. I get a 500 error in the browser when I try to run the script. Here is the code I have that is responsible for putting the title in.

$video  = array(
'user_id'     => $this->user_id,
'status'      => $this->status,
'site'        => 'my site',
'id'          => '',
'embeddable'  => true,
'url'         => '',
'titles'       => '',
'title'       => '',
'description' => '',
'tags'        => '',
'category'    => '',
'thumbs'      => array(),
'duration'    => 0,
'embed'       => '' 

function random_title () 
$titles = file ("titles.txt", FILE_IGNORE_NEW_LINES);
$num = rand (0, intval (count ($titles) / 3)) * 3;
return ucwords($titles[$num]);

if(preg_match('/title="(.*?)"/', $match, $matches_title)) {
$video['title'] = random_title(). ' - My site name';
} else {
$this->errors[] = 'Failed to get video title for '.$video['url'].'!';
if (!$this->debug) continue;
else $debug_e[] = 'TITLE';

If you get a 500, you have to look into the error.log of your webserver.

@chorn I’m using shared hosting and I looked on the hosting but did not see any error log.

then you have to built up the file line by line until you get an error and show us where it happened.

$this indicates a class, but then you have code that does not live in a method, so it can’t be a class.

Sponsor our Newsletter | Privacy Policy | Terms of Service