Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ############### |
| 2 | Trackback Class |
| 3 | ############### |
| 4 | |
| 5 | The Trackback Class provides functions that enable you to send and |
| 6 | receive Trackback data. |
| 7 | |
| 8 | If you are not familiar with Trackbacks you'll find more information |
| 9 | `here <http://en.wikipedia.org/wiki/Trackback>`_. |
| 10 | |
| 11 | Initializing the Class |
| 12 | ====================== |
| 13 | |
| 14 | Like most other classes in CodeIgniter, the Trackback class is |
| 15 | initialized in your controller using the $this->load->library function:: |
| 16 | |
| 17 | $this->load->library('trackback'); |
| 18 | |
| 19 | Once loaded, the Trackback library object will be available using: |
| 20 | $this->trackback |
| 21 | |
| 22 | Sending Trackbacks |
| 23 | ================== |
| 24 | |
| 25 | A Trackback can be sent from any of your controller functions using code |
| 26 | similar to this example:: |
| 27 | |
Derek Jones | e0da604 | 2011-10-05 15:37:39 -0500 | [diff] [blame] | 28 | $this->load->library('trackback'); |
| 29 | |
| 30 | $tb_data = array( |
| 31 | 'ping_url' => 'http://example.com/trackback/456', |
| 32 | 'url' => 'http://www.my-example.com/blog/entry/123', |
| 33 | 'title' => 'The Title of My Entry', |
| 34 | 'excerpt' => 'The entry content.', |
| 35 | 'blog_name' => 'My Blog Name', |
| 36 | 'charset' => 'utf-8' |
| 37 | ); |
| 38 | |
| 39 | if ( ! $this->trackback->send($tb_data)) |
| 40 | { |
| 41 | echo $this->trackback->display_errors(); |
| 42 | } |
| 43 | else |
| 44 | { |
| 45 | echo 'Trackback was sent!'; |
| 46 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 47 | |
| 48 | Description of array data: |
| 49 | |
| 50 | - **ping_url** - The URL of the site you are sending the Trackback to. |
| 51 | You can send Trackbacks to multiple URLs by separating each URL with |
| 52 | a comma. |
| 53 | - **url** - The URL to YOUR site where the weblog entry can be seen. |
| 54 | - **title** - The title of your weblog entry. |
| 55 | - **excerpt** - The content of your weblog entry. Note: the Trackback |
| 56 | class will automatically send only the first 500 characters of your |
| 57 | entry. It will also strip all HTML. |
| 58 | - **blog_name** - The name of your weblog. |
| 59 | - **charset** - The character encoding your weblog is written in. If |
| 60 | omitted, UTF-8 will be used. |
| 61 | |
| 62 | The Trackback sending function returns TRUE/FALSE (boolean) on success |
| 63 | or failure. If it fails, you can retrieve the error message using:: |
| 64 | |
| 65 | $this->trackback->display_errors(); |
| 66 | |
| 67 | Receiving Trackbacks |
| 68 | ==================== |
| 69 | |
| 70 | Before you can receive Trackbacks you must create a weblog. If you don't |
| 71 | have a blog yet there's no point in continuing. |
| 72 | |
| 73 | Receiving Trackbacks is a little more complex than sending them, only |
| 74 | because you will need a database table in which to store them, and you |
| 75 | will need to validate the incoming trackback data. You are encouraged to |
| 76 | implement a thorough validation process to guard against spam and |
| 77 | duplicate data. You may also want to limit the number of Trackbacks you |
| 78 | allow from a particular IP within a given span of time to further |
| 79 | curtail spam. The process of receiving a Trackback is quite simple; the |
| 80 | validation is what takes most of the effort. |
| 81 | |
| 82 | Your Ping URL |
| 83 | ============= |
| 84 | |
| 85 | In order to accept Trackbacks you must display a Trackback URL next to |
| 86 | each one of your weblog entries. This will be the URL that people will |
| 87 | use to send you Trackbacks (we will refer to this as your "Ping URL"). |
| 88 | |
| 89 | Your Ping URL must point to a controller function where your Trackback |
| 90 | receiving code is located, and the URL must contain the ID number for |
| 91 | each particular entry, so that when the Trackback is received you'll be |
| 92 | able to associate it with a particular entry. |
| 93 | |
| 94 | For example, if your controller class is called Trackback, and the |
| 95 | receiving function is called receive, your Ping URLs will look something |
| 96 | like this:: |
| 97 | |
| 98 | http://example.com/index.php/trackback/receive/entry_id |
| 99 | |
| 100 | Where entry_id represents the individual ID number for each of your |
| 101 | entries. |
| 102 | |
| 103 | Creating a Trackback Table |
| 104 | ========================== |
| 105 | |
| 106 | Before you can receive Trackbacks you must create a table in which to |
Derek Jones | e0da604 | 2011-10-05 15:37:39 -0500 | [diff] [blame] | 107 | store them. Here is a basic prototype for such a table:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 108 | |
Derek Jones | e0da604 | 2011-10-05 15:37:39 -0500 | [diff] [blame] | 109 | CREATE TABLE trackbacks ( |
| 110 | tb_id int(10) unsigned NOT NULL auto_increment, |
| 111 | entry_id int(10) unsigned NOT NULL default 0, |
| 112 | url varchar(200) NOT NULL, |
| 113 | title varchar(100) NOT NULL, |
| 114 | excerpt text NOT NULL, |
| 115 | blog_name varchar(100) NOT NULL, |
| 116 | tb_date int(10) NOT NULL, |
| 117 | ip_address varchar(16) NOT NULL, |
| 118 | PRIMARY KEY `tb_id` (`tb_id`), |
| 119 | KEY `entry_id` (`entry_id`) |
| 120 | ); |
| 121 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 122 | The Trackback specification only requires four pieces of information to |
| 123 | be sent in a Trackback (url, title, excerpt, blog_name), but to make |
| 124 | the data more useful we've added a few more fields in the above table |
| 125 | schema (date, IP address, etc.). |
| 126 | |
| 127 | Processing a Trackback |
| 128 | ====================== |
| 129 | |
| 130 | Here is an example showing how you will receive and process a Trackback. |
| 131 | The following code is intended for use within the controller function |
| 132 | where you expect to receive Trackbacks. |
| 133 | |
| 134 | :: |
| 135 | |
Derek Jones | e0da604 | 2011-10-05 15:37:39 -0500 | [diff] [blame] | 136 | $this->load->library('trackback'); |
| 137 | $this->load->database(); |
| 138 | |
| 139 | if ($this->uri->segment(3) == FALSE) |
| 140 | { |
| 141 | $this->trackback->send_error("Unable to determine the entry ID"); |
| 142 | } |
| 143 | |
| 144 | if ( ! $this->trackback->receive()) |
| 145 | { |
| 146 | $this->trackback->send_error("The Trackback did not contain valid data"); |
| 147 | } |
| 148 | |
| 149 | $data = array( |
| 150 | 'tb_id' => '', |
| 151 | 'entry_id' => $this->uri->segment(3), |
| 152 | 'url' => $this->trackback->data('url'), |
| 153 | 'title' => $this->trackback->data('title'), |
| 154 | 'excerpt' => $this->trackback->data('excerpt'), |
| 155 | 'blog_name' => $this->trackback->data('blog_name'), |
| 156 | 'tb_date' => time(), |
| 157 | 'ip_address' => $this->input->ip_address() |
| 158 | ); |
| 159 | |
| 160 | $sql = $this->db->insert_string('trackbacks', $data); |
| 161 | $this->db->query($sql); |
| 162 | |
| 163 | $this->trackback->send_success(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 164 | |
| 165 | Notes: |
| 166 | ^^^^^^ |
| 167 | |
| 168 | The entry ID number is expected in the third segment of your URL. This |
| 169 | is based on the URI example we gave earlier:: |
| 170 | |
| 171 | http://example.com/index.php/trackback/receive/entry_id |
| 172 | |
| 173 | Notice the entry_id is in the third URI segment, which you can retrieve |
| 174 | using:: |
| 175 | |
| 176 | $this->uri->segment(3); |
| 177 | |
| 178 | In our Trackback receiving code above, if the third segment is missing, |
| 179 | we will issue an error. Without a valid entry ID, there's no reason to |
| 180 | continue. |
| 181 | |
| 182 | The $this->trackback->receive() function is simply a validation function |
| 183 | that looks at the incoming data and makes sure it contains the four |
| 184 | pieces of data that are required (url, title, excerpt, blog_name). It |
| 185 | returns TRUE on success and FALSE on failure. If it fails you will issue |
| 186 | an error message. |
| 187 | |
| 188 | The incoming Trackback data can be retrieved using this function:: |
| 189 | |
| 190 | $this->trackback->data('item') |
| 191 | |
| 192 | Where item represents one of these four pieces of info: url, title, |
| 193 | excerpt, or blog_name |
| 194 | |
| 195 | If the Trackback data is successfully received, you will issue a success |
| 196 | message using:: |
| 197 | |
| 198 | $this->trackback->send_success(); |
| 199 | |
| 200 | .. note:: The above code contains no data validation, which you are |
| 201 | encouraged to add. |