Informations

Classe permetant d'envoyer un 'ping' à un serveur pour ajouter un trackback. Le serveur qui accepte le ping ajoute généralement les informations dans une base de données du serveur.

Le serveur devrait regarder l'encodage utilisé dans la variable $_SERVER['CONTENT_TYPE'].

Utilisation de la classe

require_once 'TrackBack.php';
$tb = new TrackBack;
$tb->setInfo('Le titre du billet', 'URL du billet', 'Le titre du blog', 'Le message du billet');
$tb->ping('URL distant pingé');

Classe PHP5 : TrackBack.php

class TrackBack {
 private $title   = '';   // the title of the entry
 private $url     = '';
 private $blog_name = ''; // le nom du blog
 private $excerpt = ''; // an excerpt of the entry

 private $pingHost = '';
 private $pingPort = '0';
 private $pingFileName = '';

 private $_body = '';
 private $_headers = '';

 function setInfo ($title, $url, $blog_name, $excerpt) {
  $this->title = $title; 
  $this->url = $url; 
  $this->blog_name = $blog_name; 
  $this->excerpt = $excerpt;
 } 

 private function setPingSite($site) {
   $tblurl = parse_url($site);
   if ( count($tblurl) && isset($tblurl['host']) ) {
      $this->pingHost    = $tblurl['host'];
      $this->pingPort    = isset($tblurl['port']) ? $tblurl['port'] : 80;
      $this->pingFileName= $tblurl['path'];
      if ( isset($tblurl['query']) ) {
        $this->pingFileName .= '?' . $tblurl['query'];
      }
   } else {
     $this->pingHost = $this->pingFileName = ''; 
     $this->pingPort = 0;
   }
 }

 function ping($site) {
   $this->setPingSite($site);
   $sock = fsockopen($this->pingHost, $this->pingPort, $errno, $errstr, 30);
   if (!$sock) {echo("$errstr ($errno)\n"); return false; }

   $data = 'title=' . urlencode($this->title) .
        '&url=' . urlencode($this->url) .
        '&excerpt=' . urlencode($this->excerpt) .
        '&blog_name=' . urlencode($this->blog_name).
        '&utf8=1';

   fwrite($sock, "POST ".$this->pingFileName." HTTP/1.0\r\n");
   fwrite($sock, "Host: ".$this->pingHost."\r\n");

   fwrite($sock, "Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n");
   fwrite($sock, "Content-length: " . strlen($data) . "\r\n");
   fwrite($sock, "Accept: */*\r\n");

   fwrite($sock, "\r\n");
   fwrite($sock, "$data\r\n");
   fwrite($sock, "\r\n");

   $headers = "";
   while ($str = trim(fgets($sock, 4096)))
     $headers .= "$str\n";

   $body = "";
   while (!feof($sock))
     $body .= fgets($sock, 4096);

   fclose($sock);

   $this->_body = $body;
   $this->_headers = $headers;
 }
}

Références...