Jump to content

Post to twitter with short url


SkillsToShow

Recommended Posts

I have been trying to figure this out for a while to no avail. Basically what I want to do is use this code to post to twitter, the problem is I want it to use bit.ly shortener and for that I need to use the code:

 

<?php echo $burl; ?>

 

bit.ly shortener referenced in the end.

 

How would I add it to the code below so it works correctly?

 

function twitterUpdate($postTitle, $postLink, $isNew)
{
// Enter Your Twitter ID Here
$username = 'Twitter_ID';
// Enter Your Twitter Password Here
$password = 'Password';

        # text into a twitter friendly text
$code_entities_match = array('--','"','!','@','#','$','%','^','&','*','(',')','_','+','{','}','|',':','"','<','>','?','[',']','\\',';',"'",',','.','/','*','+','~','`','=');
$code_entities_replace = array('-','','','','','','','','','','','','','','','','','','','','','','','','');
$postTitle = str_replace($code_entities_match, $code_entities_replace, $postTitle);

// Check if New or Updated Post
if($isNew)
	$postTitle = 'New Post: ' . $postTitle;
else
	$postTitle = 'Updated Post: ' . $postTitle;

// Calculate Twitter Msg and keep it under 140 Chars
        if(strlen ($postTitle) > (140 - strlen ($postLink)))
	$postTitle 		= substr_replace($postTitle, '...', (140 - 3 - strlen ($postLink)));

$message = $postTitle . $postLink;

// The twitter API address
$url = 'http://twitter.com/statuses/update.xml';
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
}

 

function postToTwitter($post_ID)
{
// Create your Short URL replace with your blog url
$postLink = ' http://clazh.com/?p=' . $post_ID; // Tried replacing this part with $burl; but I get error where the link is supposed to be
// encode the URL to fix Post to Twitter issues
        $postLink = urlencode  ( $postLink ) ; // replaced $postLink here with $burl; but still get the same error
// Get the Post Object
$get_post_info 	= get_post( $post_ID );
// Get the Post Title
$postTitle = $get_post_info->post_title;
        // Get the Post date
$postDate 		= date('U', strtotime($get_post_info->post_date));
// Get the post Modified date
$postModified 	= date('U', strtotime($get_post_info->post_modified));

// Check if the post is new or modified
if($postModified == $postDate)
{
	twitterUpdate($postTitle, $postLink, true);
}
}

// Post to twitter when you publish or update a post
add_action('publish_post', 'postToTwitter');

 

Here is the bit.ly url shortener for your refererance:

function getBitlyUrl($url) {
$bitlylogin = 'my username';
$bitlyapikey= ' my api key';
$bitlyurl = file_get_contents("http://api.bit.ly/shorten?version=2.0.1&longUrl=".$url."&login=".$bitlylogin."&apiKey=".$bitlyapikey);
$bitlycontent = json_decode($bitlyurl,true);
$bitlyerror = $bitlycontent["errorCode"];
if ($bitlyerror == 0) {
$bitlyurl = $bitlycontent["results"][$url]["shortUrl"];
}
else $bitlyurl = "error";
return $bitlyurl;
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.