Posts tagged: Web

Sending a pingback using the PHP CURL module

To send pingbacks from your site to other blogs/communities/etc., you can use the PHP CURL module:

<?
function send_pingback($sourceURI, $targetURI)
{
        $target = file_get_contents($targetURI);
        // HTTP headers are now in $http_response_header, see docs of HTTP wrapper

        foreach ($http_response_header as $header)
                if (preg_match("/^X-Pingback:\s*([^\s]+)\s*$/", $header, $matches))
                        $serverURI = $matches[1];

        if (!$serverURI) {
                $target = html_entity_decode($target);
                if (preg_match("/<link rel=\"pingback\" href=\"([^\"]+)\" ?\/?>/", $target, $matches))
                        $serverURI = $matches[1];
        }

        if (!$serverURI)
                return;

        $xml = ‘<?xml version="1.0"?><methodCall><methodName>pingback.ping</methodName><params>’.
                ‘<param><value><string>’.$sourceURI.‘</string></value></param>’.
                ‘<param><value><string>’.$targetURI.‘</string></value></param>’.
                ‘</params></methodCall>’;

        $rq = curl_init();
        curl_setopt($rq, CURLOPT_URL, $serverURI);
        curl_setopt($rq, CURLOPT_POST, 1);
        curl_setopt($rq, CURLOPT_POSTFIELDS, $xml);
        curl_exec($rq);
        curl_close($rq);
}

send_pingback("… source uri …", "… target uri …");

Receiving pingbacks using the PHP XML-RPC module

If you want to enable a Web site (for instance, a blog or community project) to receive pingbacks, you can use the PHP XML-RPC module.

All you need to do is to

  1. create an XML-RPC server
  2. register a function for pingback.ping
  3. call the server
  4. implement the function

The function itself, let’s call it do_pingback, only needs to process the pingback: verify its validity and register the ping somewhere, for instance in the database.

Sample code:

<?
/* /pingback-server.php */
require_once("include/config.php");

function do_pingback($method, $params, $extra)
{
        global $db;
        list ($sourceURI, $targetURI) = $params;

        // fetch the source URI to verify that the source does indeed link to the target
        $source = file_get_contents($sourceURI);
        if ($source == false)
                return 16;      // "The source URI does not exist."

        $source = html_entity_decode($source);
        if (strpos($source, $targetURI) === false)
                return 17;      // "The source URI does not contain a link to the target URI, and so cannot be used as a source."

        // TODO: check own data to ensure that the target exists and is a valid entry

        // register pingback (if not done already)
        $status = $db->exec("INSERT INTO pingbacks (source, target) VALUES (".$db->quote($sourceURI).", ".$db->quote($targetURI).")");
        if (PEAR::isError($status))
                return 48;

        return "Ping registered, thanks";
}


$server = xmlrpc_server_create();
xmlrpc_server_register_method($server, "pingback.ping", "do_pingback");
if ($response = xmlrpc_server_call_method($server, $HTTP_RAW_POST_DATA, null)) {
        header("Content-Type: text/xml");
        print $response;
}

?>

Of course, you have to announce the pingback server on your Web site. I prefer the HTTP header solution, so I added

header("X-Pingback: http://".GU_DOMAIN."/pingback-server.php");

to a global include.

I use this code for the pingback implementation on gimpusers.com / gimpusers.de.

HTML/CSS lists with markers on the right side

If you need a HTML list with the markers (dots, squares, list-style-images, whatever you use) on the right side (for instance for a navigation menu that is aligned to a line on its right side), you can achieve this by using RTL (right-to-left) text:

<ul style="direction: rtl;">
    <li>aligned to the right side</li>
    <li>yes, it is</li>
</ul>

Demo:

  • aligned to the right side
  • yes, it is

3/3 Populate an AJAX AutoSuggest field on your Web site with myisam_suggest

In “1/3 Implementing an AutoSuggest feature using MySQL fulltext indices” I described how you can use a MyISAM full-text index to extract search words for an AutoSuggest feature. Then, in “2/3 myisam_suggest: an AutoComplete tool for MySQL fulltext indices” I wrote how to install and use myisam_suggest to extract these search words in practice.

Here you will find how to use AJAX AutoSuggest together with my tool “myisam_suggest”.

For a demo, see www.gimpusers.com. Enter some characters (e.g. “brus”) into the search field on the top of the page.

Read more »

2/3 myisam_suggest: an AutoComplete tool for MySQL fulltext indices

As I’ve written in my previous post “1/3 Implementing an AutoSuggest feature using MySQL fulltext indices”, it’s possible to use the MySQL/MyISAM full-text index to extract search words for an AutoSuggest feature with great performance (because the index tree is used actually). This tool, called myisam_suggest, is my first implementation of this.
Read more »

Image | WordPress Themes