Update To The YOURLS – Twitter – Google Reader Script

I recently blogged, Using YOURLS And The Twitter API With Google Reader’s Custom SendTo Link. Since then, I have made a few improvements to the script, mostly in the error-trapping line.

  • I broke out the variables for the YOURLS API signature, plus the Twitter API consumer key, consumer secret, access key and access secret, and converted them to constants.
  • I removed urldecode() from the formation of the YOURLS API request. It’s not necessary.
  • I have added code to trap any errors in when making the short URL.
    • I am now getting the statusCode and message node contents, in case something goes wrong with the shortening.
    • I set the script to die on a URL shortening error, which is determined by the short URL being an empty string or the statusCode node having a value other than 200.
    • The code, coupled with the message, should explain any problems adequately enough to debug shortening issues. If you find you are getting zero-length responses from your URL shortening, that’s probably due to a server misconfiguration; possibly from bad mod_rewrite rules, possibly due to a messed-up cookie on your PC.
  • I strip HTML tags from the title.
    • I discovered today that some Google Reader items will have HTML markup in their titles, such as <em>.
  • I check for failure in the Twitter API request.
    • TwitterOAuth will return Boolean false if it could not complete its curl request to the API, and will return an XML document in any other case, including failure of the tweet to go through.
    • If the cUrl request is good but the tweet doesn’t go through, Twitter responds with an HTTP status code other than 200. We can capture the last status code returned by our request, test it, and print the XML response describing what happened.

Here’s the corrected code:

Using YOURLS And The Twitter API With Google Reader's Custom SendTo Link
Copyright (C) 2010 Doug Vanderweide

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

//YOURLS API signature
define('YOURLS_API_SIGNATURE', 'your_yourls_api_signature');

//twitter API keys
define('TWITTER_API_CONSUMER_KEY', 'your_consumer_key');
define('TWITTER_API_CONSUMER_SECRET', 'your_consumer_secret');
define('TWITTER_API_ACCESS_TOKEN', 'your_access_token');
define('TWITTER_API_ACCESS_TOKEN_SECRET', 'your_access_secret');


//sanitize inputs
if(!isset($_GET['url'])) {
	die('Could not find URL of item');
elseif(trim($_GET['url']) == '') {
	die('URL is empty string');
elseif(!isset($_GET['title'])) {
	die('Could not find title of item');
elseif(trim($_GET['title']) == '') {
	die('Title is empty string');
else {
	//build GET link to YOURLS API
	$link = 'http://url.dougv.com/yourls-api.php?signature=' . YOURLS_API_SIGNATURE . '&action=shorturl&format=xml&url=' . $_GET['url'];

	//use curl to send API request
	//reads XML response into string
	$ch = curl_init($link);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	//if curl fails, report that fact
	if(!$reply = curl_e xec($ch)) {
		die('Curl request to URL shortening service failed.');

	//create DOMDocument to parse response string
	$xml = new DOMDocument();

	//get short URL
	$node = $xml->getElementsByTagName('shorturl')->item(0);
	$shorturl = $node->textContent;

	//get status message
	$node = $xml->getElementsByTagName('statusCode')->item(0);
	$status = $node->textContent;

	//get verbose API response
	$node = $xml->getElementsByTagName('message')->item(0);
	$message = $node->textContent;

	//if the short URL is an empty string, or the status code isn't 200,
	//kill the script and report error
	if(trim($shorturl) == '' || $status != '200') {
		die("Shortening of url failed. Error code: $status Error message: $message");

	//set twitter status
	$status = strip_tags($_GET['title']) . " " . $shorturl;

	//create TwitterOAuth object
	//send status update request
	//if curl fails, report that
	if(!$content = $to->OAuthRequest('https://twitter.com/statuses/update.xml', array('status' => $status), 'POST')) {
		$content = 'Failure posting status to Twitter. This is probably due to a network error, downtime in the Twitter API, etc.';
	//if the Twitter API has a fit, report that
	elseif($to->lastStatusCode() != '200') {
		$content = 'Error returned by Twitter API. Code: ' . $to->lastStatusCode() . ' // XML response: // ' . print_r($content, true);
	//redirect to Twitter on success
	header('Location: http://www.twitter.com/home');

I have repacked the download code, which now contains this version of the script. I distribute code under the GNU GPL.

2 thoughts on “Update To The YOURLS – Twitter – Google Reader Script

  1. Pingback: Using YOURLS And The Twitter API With Google Reader’s Custom SendTo Link – dougv.com « Doug Vanderweide

  2. Pingback: » Using YOURLS And The Twitter API With Google Reader’s Custom SendTo Link dougv.com « Doug Vanderweide

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>