PHP Code Broken in WordPress

I just spent the last few days trying to figure out something odd. When I pasted certain lines to post in WordPress, it would cause a 503 error. I eventually narrowed it down to a few particular lines in my last example:

$mydebug = fopen('debug.txt','a'); in a post it causes a 503 error
$file_contents = curl_exec($ch);
fwrite($mydebug,"/nHttp Code: $httpCode/n");

Each one would throw a Service Unavailable 503 error. I got around the problem by tossing a space between the parenthesis and semi-colon but it still bothered me.

Turns out it wasn’t WordPress at all, but rather the Apache module mod_security. This post tells the whole story. The quick fix is to create/modify your wp-admin .htaccess file to include:

SecFilterEngine Off

After that I could easily post this message without flubbing my code.

YouTube Uploader


This script no longer works. The correct way to do this now is to use YouTube’s API.

There are plenty of tutorials on how to use YouTube to get content. But after much searching I couldn’t find a way to upload videos to Youtube.

So with the help of a friend to get me started, and some diligent searching to fill in the missing pieces, I came up with this code.

It’s important to note that this is article is geared towards experienced programmers. If you are a typical user trying to upload your best bet is to use YouTube’s webpage. You will need to understand and modify some parts of this code in order to get it working in your environment.

Below is the main function that does the dirty work. We are using curl for our uploading. I’ve tried commenting the best I can.

The Posting Function

<?php

define('DEBUG','FALSE');

function http_post_form($url, $vars) {
  $ch = curl_init();
  $timeout = 0; // set to zero for no timeout
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return to string instead of spewing to output
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  // follow location header, not sure if this is needed.
  // I like chocolate chip
  curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
  curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");

  // Expect: 100-continue doesn't work properly with lightTPD
  // This fix by zorro http://groups.google.com/group/php.general/msg/aaea439233ac709b
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); 

  // Debugging
  if(defined(DEBUG) && DEBUG == TRUE) {
    $mydebug = f-open('debug.txt','a');
    curl_setopt($ch, CURLOPT_STDERR, $mydebug);
    curl_setopt($ch, CURLOPT_VERBOSE, 1); 
  }

  // Set method post
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);

  $file_contents = curl_e xec($ch);

  if(defined(DEBUG) && DEBUG == TRUE) {
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    f write($mydebug,"/nHttp Code: $httpCode/n");
  }

  curl_close($ch);

  if(defined(DEBUG) && DEBUG == TRUE) {
    f close($mydebug);
  }

  return $file_contents;
}

?>

That function essentially does the job of filling out a form and submitting it to the server for us. While there are a few lines of code to get it working with YouTube’s servers this function could certainly be useful for any automated posting means.

Logging In

The first step in our YouTube journey is logging in. $user and $password needs to be replaced with your YouTube login information.

$user = 'user';
$password = 'password';

/* don't touch these, we are just setting up our ugly strings with our information to pass onto our super function above. */
$param = 'current_form=loginForm&next=/my_videos_upload%3F&username='.$user.'&password='.$password.'&action_login=Log+In';
$url = 'http://www.youtube.com/signup?next=/my_videos_upload%3F';

http_post_form($url, $param);

Posting Your Video

$title, $description and the filename of the video to be uploaded change every time. I insert this information using a simple form webpage. The other information stays the same for all of my ditties. You will need to adjust the variables for your situation.
$file is set to the path of the file to be uploaded to YouTube. (The file should already be uploaded to your server.)

/* Change me ! */
$title = stripslashes($_POST['title']);
$description = stripslashes($_POST['description']);
$keywords = 'insert your keywords here';
$file = '../uploads/' . $_POST['file'];
$privacy = 'private';
$category = '10';
$language = 'EN'

/* stop changing */
$post_vars = array();
$post_vars['field_privacy'] = $privacy;
$post_vars['field_myvideo_keywords'] = $keywords;
$post_vars['field_myvideo_title'] = $title;
$post_vars['field_myvideo_descr'] = $description;
$post_vars['field_myvideo_categories'] = $category;
$post_vars['language'] = $language;
$post_vars['field_uploadfile'] = "@$file";
$post_vars['field_command'] = 'myvideo_submit';
$post_vars['submit'] = 'Upload%20Video';

/* See note below about YouTube URL's */
$url = 'http://v##.youtube.com/my_videos_post';

$return = http_post_form($url, $post_vars);

Please note I’m fairly sure YouTube gives a random server number for the url listed above. As far as I know your safe using v1-v64. Please choose a number at random, come up with some nifty random function, or best yet figure out exactly what YouTube is up to and come up with the best solution.

For convenience YouTube’s current settings are copied below.

2
Autos & Vehicles
23
Comedy
24
Entertainment
1
Film & Animation
20
Gadgets & Games
26
Howto & DIY
10
Music
25
News & Politics
22
People & Blogs
15
Pets & Animals
17
Sports
19
Travel & Places
EN
English
ES
Spanish
JP
Japanese
DE
German
CN
Chinese
FR
French
field_privacy
public|private