We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34193
    • 330 Posts
    OK so here is the updated code at the moment it pings both the google sitemap and pingomatic, I will work on giving the option of one or both. It is definitely communicating with the pingomatic server as I am getting positive responses back from it.

    I will try and package this but I can’t find away to get packman to include just tv’s without a template if someone can inform me how to do this it would be usefull.

    <?php
    function pingGoogleSitemaps( $url_xml )
    {
       $status = 0;
       $google = 'www.google.com';
       if( $fp=@fsockopen($google, 80) )
       {
          $req =  'GET /webmasters/sitemaps/ping?sitemap=' .
                  urlencode( $url_xml ) . " HTTP/1.1\r\n" .
                  "Host: $google\r\n" .
                  "User-Agent: Mozilla/5.0 (compatible; " .
                  PHP_OS . ") PHP/" . PHP_VERSION . "\r\n" .
                  "Connection: Close\r\n\r\n";
          fwrite( $fp, $req );
          while( !feof($fp) )
          {
             if( @preg_match('~^HTTP/\d\.\d (\d+)~i', fgets($fp, 128), $m) )
             {
                $status = intval( $m[1] );
                break;
             }
          }
          fclose( $fp );
       }
       return( $status );
    }
    
    function pingomatic($title,$url,$debug=false,$modx) {
      /* Code in this function thanks to 
      *  http://www.barattalo.it/2010/02/24/ping-pingomatic-com-services-with-php/
      */
        $content='<?xml version="1.0"?>'.
            '<methodCall>'.
            ' <methodName>weblogUpdates.ping</methodName>'.
            '  <params>'.
            '   <param>'.
            '    <value>'.$title.'</value>'.
            '   </param>'.
            '  <param>'.
            '   <value>'.$url.'</value>'.
            '  </param>'.
            ' </params>'.
            '</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) $modx->log(modX::LOG_LEVEL_DEBUG,'Debug Response :: '.$response);
            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) $modx->log(modX::LOG_LEVEL_DEBUG,'Debug Response :: '.$errstr.' ('.$errno.')');
            return array('status'=>'ko', 'msg'=>$errstr." (".$errno.")");
        }
    }
    
    $modx->getService('fire', 'modFire', $modx->getOption('core_path').'components/modfire/');
    
    $notify = false;
    
    $eventName = $modx->event->name;
    switch($eventName) {
        case 'OnDocFormSave':
            if ($resource->published==1 && $resource->getTVValue('NotifyGoogle')){
                $notify = true;
            }
            else {
                $notify = false;
            }
            break;
        case 'OnDocPublished':
            if ($resource->getTVValue('NotifyGoogle')){
                $notify = true; 
            }
            else {
                $notify = false;
            }
            break;
    }
    
    if ($notify) {
        $url_xml = $modx->config['site_url'].'/sitemap.xml';
        $returncode = pingGoogleSitemaps( $url_xml );
        if (!$returncode == 200) {
             $modx->log(modX::LOG_LEVEL_DEBUG, 'There was an error connecting to the google server it returned the following code '.$returncode);
        } 
        $url = $modx->config['site_url'].$modx->resource->uri;
    
        $response = pingomatic($modx->resource->pagetitle,$url,false,$modx);
      
        if ($response['status'] == 'ko') {
             $modx->log(modX::LOG_LEVEL_DEBUG, 'There was an error connecting to the pingomatic site it returned the following message '.$response['msg']);
        } 
    }


    EDIT :- hmm the code highlighter doesn’t like this line
    $content=’<?xml version="1.0"?>’.
    ’<methodCall>’.
    ’ <methodName>weblogUpdates.ping</methodName>’.
    ’ <params>’.
    ’ <param>’.
    ’ <value>’.$title.’</value>’.
    ’ </param>’.
    ’ <param>’.
    ’ <value>’.$url.’</value>’.
    ’ </param>’.
    ’ </params>’.
    ’</methodCall>’;

    and removes the initial ’
      • 34193
      • 330 Posts
      OK submitted this as an extra minus the site map bit since I feel the pingomatic.com bit is the key here. I also found this http://www.hyper-typer.com/news/search-engine-sitemap-update-ping-modx-plugin which I think with permission I could adapt to work with the pingomatic script which would then give further feed back on submission which at the moment you don't get any indication.
        • 34193
        • 330 Posts
        The extra has been accepted and can be downloaded form http://modx.com/extras/package/pingomatix or via the package manager under Extras -> Blogging -> PingoMatiX .

        Improvements to follow.

        UPDATE : OK added this to github see https://github.com/peteedley/PingoMatiX for latest code, also bugs and feature requests to https://github.com/peteedley/PingoMatiX/issues [ed. note: pedley last edited this post 15 years, 2 months ago.]