5ubliminal@twitter

I want Pingback in my custom built Blogs! : 5ubliminal's TellinYa

<a href="http://www.tellinya.com/art2/173/">I want Pingback in my custom built Blogs! : 5ubliminal's TellinYa</a>
Must Reads: Web Scraping | Link Farming | Code Snippets | SEO Freeware » I'm on vacation! … still alive :)
Reveal More!
What is pingback?

You will notice in many blogs, in the comments section, many comments like this: Pingback from …. What is a pingback and how it differs from regular comments even if they are listed in same section?

I pingback is a pat on the shoulder from one blog to another where first says: Hey, psssst, blog! I mentioned you in a post, care to mention me back? The second blog may choose to post a comment (pingback comment) where it will take the title and a little excerpt from the first blog. The pinback specification can be found here but it's lame and explains nothing technical!

Had to find a solution!

As I built my custom blog engine I wanted to integrate pingback. Pingback relies on XMLRPC and you can read more here and here! I'll explain the concept in a nutshell: XMLRPC allows one to send commands in XML to a remote script by using HTTP POST and have it execute stuff! This is how pingback works. You send a command to the rpc client in the blog and the blog will choose the further steps.

How Pingback works?

These are the steps you have to take:

Step1: Load the page you link to and discover any RPC client. In the HTTP header will look like this:
X-Pingback: http://example.com/pingback/xmlrpc and in the HTML body it will look like this:
<link rel="pingback" href="http://example.com/pingback/xmlrpc" />

Step2: Notify the XMLRPC server of your link by sending a RPC pingback command.

Step3: Pray it'll pass moderation and appear in the blog :)

Warning: This is not a good link spam method unless you have some real content and the pingback passes moderation.

Now the technical PHP part!

I downloaded WordPress and the code inside is really not my type! Not clean and nice formatted as I enjoy PHP scripts. I have no time to see how WordPress works, I'd rather rebuild it myself! So … I kept looking and finally got to the solution I needed which is a two step trick. You have to autodiscover the RPC client and issue the command.

Below is the PHP script which relies on the XMLRPC integrated in PHP. I might make it standalone but I wrote this code yesterday. This code needs PHP cUrl Class previously declared!

<?
//---------------------------------------------------
// This is the autodiscovery of XMLRPC client. Can be used alone.
function findXmlRpc($target){
    
$hc=new eHttpClient(); $hc->get($target,0,1); $hdr $hc->getHeader();
    return (isset(
$hdr['X-Pingback']) ? $hdr['X-Pingback'] : false);
}
//---------------------------------------------------
// Change $returnbool to 0 to get exact XMLRPC response and error codes
function pingBack($target,$source,$returnbool=1){
    
$xmlrpc findXmlRpc($target);
    if(
$xmlrpc === false) return false;
    
$request xmlrpc_encode_request("pingback.ping", array($source,$target));
    
$http_info = array(
        
'method' => "POST",
        
'header' => "Content-Type: text/xml",
        
'content' => $request
    
);
    
$context stream_context_create(array('http' => $http_info));
    
$file file_get_contents($xmlrpcfalse$context);
    
$response xmlrpc_decode($file);
    if(
$returnbool) return (xmlrpc_is_fault($response) ? false true);
    if(
xmlrpc_is_fault($response))
        return array(
$response['faultString'],$response['faultCode']);
    return 
$response;
}
//---------------------------------------------------
?>

How to use the script:

<?
$success
=pingBack("http://www.theblogyoulinkto.com/page","http://www.theblogyoulinkfrom.com/page");
?>

Easy right? It took me over 2 hours to find specs to get this done as I love to reinvent the better wheel :) N-Joy!

11 Comments Posted By Readers :

Add your comment
#1 unistrani from Italy web
Posted on Friday, 25 April, 2008
Nice website, exist something in ASP?
#2 5ubliminal web
Posted on Saturday, 26 April, 2008
No:) It's PHP. And it's self made so … unique.
#3 muskokee from Canada
Posted on Sunday, 25 May, 2008
Hi 5ubliminal,
First, your site is a god-send! Full of great info and code. I am looking to add your trackback and ping code to my modx installation but was wondering if there was a way to receive pings from other blogs? I can code something for trackback receiving but am totally lost with pings.
Thanks
Sheri
#4 5ubliminal web
Posted on Sunday, 25 May, 2008
It's easy. Create a test.php page. Ping it with my script, see the data you receive (It's XML), parse it and get it in the database.
Use simplexml_load_string to parse. It should be easy. Lemme know if you need anything else xplained.

If you talk about trackbacks, those are just _GET variables. Easier.
#5 muskokee from Canada
Posted on Monday, 26 May, 2008
Hi 5ubliminal, it *should* be easy, but my brain is working on half-load right now. I understand the parsing function...but the function needs a variable to parse. Is this simply $_POST or something else?

Thanks again
Sheri
p.s. I got the sending of trackbacks and pings up and running (pinged a wordpress test site)...man was that easy!
#6 5ubliminal web
Posted on Monday, 26 May, 2008
:) I'll change this message tomorrow to a solution for you. It's not the POST you have to parse but the raw posted data.
I have that written somewhere down but I can't find it right now.

PS: Try this first: $data = file_get_contents('php://input'); . I know another version but try this first and let me know.
#7 muskokee from Canada
Posted on Tuesday, 27 May, 2008
So strange...I put a comment through but it never showed up! Anyway, I tried 'file get contents' but it placed nothing in the database. I did 'readfile("php://input");' just to be sure and it came up with a length of 0. Just using the php pingback script from above. I know it works because Wordpress receives it. Maybe I should just dig into the wordpress ping receiving script and rework it for modx.

Sheri
#8 5ubliminal web
Posted on Tuesday, 27 May, 2008
Check this page out: Raw POST Data. Maybe your PHP is older. I use 5.
#9 muskokee from Canada
Posted on Friday, 30 May, 2008
Hi 5ublimlinal,

This is the code that finally worked. My php is 5.2.2 and there is a bug that affects HTTP POST apparently. Not sure why this works, but it does:

if (!isset($HTTP_RAW_POST_DATA)){
$HTTP_RAW_POST_DATA = file_get_contents('php://input');
}

Even though it uses the same function, somehow it makes a difference. I cobbled a xml-rpc server together for pings using the IXR library. I'm sticking with your class for the client - it's just too easy to use! I have adjusted the findXmlRpc function to check for a link tag when x-pingback is not set in the header:

function findXmlRpc($target){
$hc=new eHttpClient();
$hc->get($target,0,1);
$hdr = $hc->getHeader();
if ($hdr['X-Pingback']){
$pingbackserver = $hdr['X-Pingback'];
}else{
$html = $hc->get($target);
preg_match("//i", $html, $matches);
$pingbackserver = $matches[1];
}
return (isset($pingbackserver) ? $pingbackserver : false);
}

Thanks for all your help :)
Sheri
#10 nogenius from United States
Posted on Friday, 08 August, 2008
Hi 5ubliminal,

I was hoping you could help me out with a baffling problem I have recently encountered with sending Pingbacks.

I have been using the code snippet for sending Pingbacks you provided above, and it has worked great (for many, many pingbacks, we are talking thousands here). However, there is one URL that I cannot seem to pingback.

Whenever I try to pingback this URL, my script hangs and starts taking up massive amounts of CPU. In my script I have also set the set_time_limit function to a finite value and have set the timeout parameter in the stream context to 30 seconds. However in spite of that, the script still hangs and will not end. With other pingback URLs, it works perfectly fine with no problems.

I am not sure if you would be OK with me posting the URL publicly in your blog, so if you would like to help me with this issue, I can either post the URL or email it to you.

Any help would be appreciated, I have literally no idea what is causing this.

Thanks,
nogenius
#11 5ubliminal web
Posted on Saturday, 09 August, 2008
I'm currently in vacation but will be back in a few days.
I'll notify you by mail when I return and you send me the URL in return to test it.

I'll see what the problem is (if there's any) and fix it.
Cheers and enjoy summer.

PS: You can post URL's here. They won't work as links ;) But if you don't want it visible send it to me when I return.
Post Feedback 
Name *
Mail *
URL
« Anti-Spam
» URL will only go live after a review. Comments are moderated. «
5ubliminal's TellinYa.com SEM & SEO Blog © 2007 - All rights reserved unless mentioned otherwise .
Rendered On : [Thursday, 21 August, 2008 - 20:43:08 GMT]   No Ajax / Flash Used Here
" I want Pingback in my custom built Blogs! : 5ubliminal's TellinYa "