Open Trackback Friend

Open Trackback Friend

Trackback是什么,自己看 TrackBack – 維基百科。 直接发送Trackback 可以干什么,自己想(不要作恶)。以下分享3种不同语言 直接发送Trackback的代码片断。

3种语言分别是 Python、PHP、ASP.net, 如果你直接运行这些脚本,期望发送至不同的博客,那么你肯定会很失望,这些只是例子,并且不完整; 我也不会把如何搭建可运行环境的过程写出来,请自己Google。

1. PHP版 (运行需要 pear,依赖Services_Trackback, Services_Trackback依赖HTTP_Request2, HTTP_Request2依赖Net_URL2 )

<?php
$trackbackData = array(
    'title'     => 'Trackback Test',
    'excerpt'   => 'Where is my frends? ...',
    'url'       => 'http://mifunny.info/friends',
    'blog_name' => 'My Blog',
    'trackback_url' => 'http://mifunny.info/how-to-obtain-the-flow-of-foreign-brush-google-adsense-229.html/trackback'
);

// include class
include "Services/Trackback.php";

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

// set object properties
foreach ($trackbackData as $k => $v) {
  $trackback->set($k, $v);
}

// send trackback
$ret = $trackback->send();
if (PEAR::isError($ret)) {
  echo "ERROR. Trackback failed because: " . $ret->getMessage();
} else {
  echo "Trackback successfully posted";
}
?>

 
2. Python版 (需要 tblib函数库 )

import tblib
tblib.TrackBack()
tb.autodiscover('http://mifunny.info/how-to-obtain-the-flow-of-foreign-brush-google-adsense-229.html/trackback')
print tb.tbUrl
tb.blog_name = 'My Weblog'
tb.title = 'Trackback Test'
tb.url = 'http://mifunny.info/friends'
tb.excerpt = 'Where is my frends? ...'
tb.ping()
print tb.tbErrorCode

 
3. ASP.net版 (LD不懂ASP,仅仅列出代码)

        //// 作 用:向指定的URL发送Trackback Ping,并根据服务器端返回的信息,提示用户处理情况。参数必须Server.URLEncode
        ///目标URL,也即所引用的blog所提供的引用地址
        ///我的Blog的URL
        ///我的blog站点名称
        ///当前这篇blog的标题
        ///当前这篇blog的摘要
        /// 返回结果:字符串,以“|”分隔,第一部分为数字,0表示成功,1表示有错;第二部分是具体信息。
        public static int Send(string RemoteUrl, string MyBlogURL, string MyBlogName, string MyBlogTitle, string MyBlogExcerpt)
        {
            // 构造要发送的请求内容
            try
            {
                string strPostInfo = “title=” + MyBlogTitle;
                strPostInfo += “&url=” + MyBlogURL;
                strPostInfo += “&excerpt=” + MyBlogExcerpt;
                strPostInfo += “&blog_name=” + MyBlogName;

                byte[] strs = System.Text.Encoding.Default.GetBytes(strPostInfo);

                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(RemoteUrl);

                myRequest.Method = “POST”;
                myRequest.ContentType = “application/x-www-form-urlencoded”;
                myRequest.ContentLength = strs.Length;
                Stream newStream = myRequest.GetRequestStream();
                // 发送数据
                newStream.Write(strs, 0, strs.Length);
                newStream.Close();
                return 1;
            }
            catch (System.Exception es)
            {
                return 0;
            }
        }