Create XML pingomatic for your PHP project

Ping-O-Matic is a service to update different search engines that your blog has updated.

Here is simple way to do that:

 <?php
/**
 * XML Pingomatic
 *
 * @category  Model
 * @author    Md. Sirajus Salayhin <salayhin@gmail.com>
 *
 */
 
class xmlPingoMatic
{
    public function pingoMatic($debug = false)
    {
 
        $sql = "SELECT DISTINCT * ,DATE_FORMAT(date, '%Y-%m-%dT%H:%i:%s+00:00') AS new_date FROM articles
                    WHERE articles.status =  0
                    AND DATE_FORMAT(date, '%Y-%m-%d') = CURDATE() ORDER BY articles.id DESC";
        $res = mysql_query($sql);
 
 
        while ($row = mysql_fetch_assoc($res)) {
 
            $data[] = $row;
        }
 
        $content = '';
        $content .= '<?xml version="1.0"?>';
        $content .= '<methodCall>';
        $content .= '<methodName>weblogUpdates.ping</methodName>';
        $content .= '<params>';
 
        for ($i = 0; $i < count($data); $i++) {
            $content .= '<param>';
            $content .= '<value>' . $data[$i]['title'] . '</value>';
            $content .= '</param>';
            $content .= '<param>';
            $content .= '<value>' . 'http://www.yourdomain.com/article.php?id=' . $data[$i]['id'] . '</value>';
            $content .= '</param>';
        }
 
        $content .= ' </params>';
        $content .= '</methodCall>';
 
        $headers = "POST / HTTP/1.0\r\n" .
            "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5 (.NET CLR 3.5.30729)\r\n" .
            "Host: rpc.pingomatic.com\r\n" .
            "Content-Type: text/xml\r\n" .
            "Content-length: " . strlen($content);
 
        if ($debug) nl2br($headers);
 
        $request = $headers . "\r\n\r\n" . $content;
        $response = "";
        $fs = fsockopen('rpc.pingomatic.com', 80, $errno, $errstr);
        if ($fs) {
            fwrite($fs, $request);
            while (!feof($fs)) $response .= fgets($fs);
            if ($debug) echo "<xmp>" . $response . "</xmp>";
            fclose($fs);
            preg_match_all("/<(name|value|boolean|string)>(.*)<\/(name|value|boolean|string)>/U", $response, $ar, PREG_PATTERN_ORDER);
            for ($i = 0; $i < count($ar[2]); $i++) $ar[2][$i] = strip_tags($ar[2][$i]);
            return array('status' => ($ar[2][1] == 1 ? 'ko' : 'ok'), 'msg' => $ar[2][3]);
        } else {
            if ($debug) echo "<xmp>" . $errstr . " (" . $errno . ")</xmp>";
            return array('status' => 'ko', 'msg' => $errstr . " (" . $errno . ")");
        }
 
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s