Jump to content

what is my mistake in this class ?


cyber_alchemist
Go to solution Solved by cyber_alchemist,

Recommended Posts

I was trying to write a class which would generate a sitemap for every post which is made or edited, but i don't seem to understand what is my mistake here.

class sitemap {
var $file_net;
var $url_net;
var $extention_net;
var $freq_net;
var $priority_net;

function set() {
  $file = $this->file_net;
  $url = $this->url_net;
  $extention = $this->extention_net;
  $freq = $this->freq_net;
  $priority = $this->priority_net;
}

function Path ($p)
{
    $a = explode ("/", $p);
    $len = strlen ($a[count ($a) - 1]);
    return (substr ($p, 0, strlen ($p) - $len));
}

function GetUrl($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

function Scan($url)
{
    global $scanned, $pf, $extension, $skip, $freq, $priority;
    
    echo "scan url $url\n";

    array_push ($scanned, $url);
    $html = GetUrl ($url);
    $a1 = explode ("<a", $html);

    foreach ($a1 as $key => $val)
    {
  $parts = explode (">", $val);
  $a = $parts[0];
  
  $aparts = explode ("href=", $a);

  $hrefparts = explode (" ", $aparts[1]);
  $hrefparts2 = explode ("#", $hrefparts[0]);

  $href = str_replace ("\"", "", $hrefparts2[0]);
  
  if ((substr ($href, 0, 7) != "http://") && 
     (substr ($href, 0,  != "https://") &&
     (substr ($href, 0, 6) != "ftp://"))
  {
      if ($href[0] == '/')
    $href = "$scanned[0]$href";
      else
    $href = Path ($url) . $href;
  }
  
  if (substr ($href, 0, strlen ($scanned[0])) == $scanned[0])
  {
      $ignore = false;
      if (isset ($skip))
    foreach ($skip as $k => $v)
        if (substr ($href, 0, strlen ($v)) == $v)
      $ignore = true;
      
      if ((!$ignore) &&
    (!in_array ($href, $scanned)) && 
    (strpos ($href, $extension) > 0)    
    )
      {
    fwrite ($pf, "<url>\n  <loc>$href</loc>\n" .
           "  <changefreq>$freq</changefreq>\n" .
           "  <priority>$priority</priority>\n</url>\n");
    echo $href. "\n";
    Scan ($href);
      }
  }
    }
}

            


    $pf = fopen ($file, "w");
    if (!$pf)
    {
  echo "cannot create $file\n";
  return;
    }

    fwrite ($pf,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<urlset
      xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"
      xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
      xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">
<!-- created with SiteMap Generator -->

<url>
  <loc>$url/</loc>
  <changefreq>daily</changefreq>
</url>
");

    $scanned = array();
    Scan ($url);
    
    fwrite ($pf, "</urlset>\n");
    fclose ($pf);

}
Link to comment
Share on other sites

  • Solution

nevermind , i overlooked 

$pf = fopen ($file, "w");
    if (!$pf)
    {
  echo "cannot create $file\n";
  return;
    }
 
    fwrite ($pf,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<urlset
      xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"
      xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
      xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">
<!-- created with SiteMap Generator -->
 
<url>
  <loc>$url/</loc>
  <changefreq>daily</changefreq>
</url>
");
 
    $scanned = array();
    Scan ($url);
    
    fwrite ($pf, "</urlset>\n");
    fclose ($pf);
 

i was writing this in free style previously

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.