Sending a trackback

17 August 2009

Sending a trackback will take place in the admin side of your website when you submit a blog entry.

In your admin area you will often have a field to enter trackback URLs.

When you then click submit a trackback will be sent to each of these trackback URLs.

The receiving trackback URL will then send you a success or error response to notify you of the trackback status.

Once the blog entry has been submitted I save all the blog data to the database, along with the trackback URLs that you want to send to.

I personally then have a separate page that I then go to which carries out the trackbacks after getting all the trackback URLs back out of the database where we just saved them.

Here's my code which sends the trackbacks:

<?php
if (isset($_REQUEST['id'])) //Get the id of the blog having the tracback sent to it
{

include "database/connectdatabase.php"; //Connect to the database
include "Services/Trackback.php";

$query = "SELECT blog_id,description,ping_links,title,link FROM blog WHERE live = 'on' AND id = '" . $_REQUEST['id'] . "'"; //Get the blog information to send
$result = mysql_query($query);
$row = mysql_fetch_array($result);

$site_query = "SELECT name FROM site WHERE name <> '' LIMIT 1";
$site_result = mysql_query($site_query);
$site = mysql_fetch_array($site_result);

$title = $row['title']; //The title of the blog
$excerpt = $row['description']; //The blog description

$url = “http://www.peternichol.com/blog/entry/" . $row['link'] . "/"; //The blog URL

$blog_name = $row['title']; //Not too sure on the difference between this and the $title variable, but I've put the blog title again

$trackback_url = "http://www.peternichol.com/entry/trackback/" . $_REQUEST['id'] . "/"; //The trackback URL for this particular blog entry – although I don't think this is used on outgoing trackbacks

$ping_servers = explode("n",$row['ping_links']); //From the trackback URLs it separates them (one per line)
mysql_close();

foreach ($ping_servers as $server) //Loop each of the trackback URLs
{

echo "Value: $value<br />n"; //Show the trackback URL we're sending to
$trackbackData = array( //Set the trackback data
'id' => "",
'title' => $title,
'excerpt' => $excerpt,
'url' => $url, //My blogs URL
'blog_name' => $blog_name,
'trackback_url' => $server
);

//new trackback instance
$trackback = new Services_Trackback();

//set opbject properties
foreach ($trackbackData as $k => $v)
{

$trackback -> set($k, $v);
}

//send trackback
$ret = $trackback -> send();
if (PEAR::iserror($ret))
{

echo "<strong>" . $server . "</strong> ping failed" . $ret -> getMessage() . "<br />"; //Trackback failure error
}
else
{
echo "<strong>" . $server . "</strong> ping successful<br />";
//Trackback sent successfully
}

}

}
else
{

echo "<p>An error occurred, pings not sent</p>"; //Could not send trackback

}
?>

http://www.peternichol.com/entry/trackback/135/

Please leave a comment using the form provided.