PHP search script, UK sites only.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

PHP search script, UK sites only.

Post by tomsace »

Hey, I have a search engine website and I wondered how I could filter the search results to display just sites from the UK only. Or just .co.uk sites only.

Thanks.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP search script, UK sites only.

Post by Benjamin »

Would you like to post your search code so we can help you modify it?
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: PHP search script, UK sites only.

Post by tomsace »

This is the msn feeds file.

Code: Select all

<?
 
 
  function getmsnarray ($SearchTerm, $Page, $NumberItems, $showmin, $PID, $lang)
  {
    global $xmlItems;
    global $xmlItemsNew;
    global $Percent;
    global $xmlListingName;
    global $xmlFields;
    $Percent = $BidPercent;
    $xmlItems = array ();
    $xmlItemsNew = array ();
    $SearchTerm = str_replace (' ', '+', $SearchTerm);
    $ip = $_SERVER['REMOTE_ADDR'];
    if ($lang == 'fr')
    {
      $strQuery = '' . 'http://search.msn.fr/results.aspx?q=' . $SearchTerm . '&format=rss&FORM=RSRE&count=' . $NumberItems . '&first=' . $showmin;
    }
    else
    {
      if ($lang == 'de')
      {
        $strQuery = '' . 'http://search.msn.de/results.aspx?q=' . $SearchTerm . '&format=rss&FORM=RSRE&count=' . $NumberItems . '&first=' . $showmin;
      }
      else
      {
        if ($lang == 'it')
        {
          $strQuery = '' . 'http://search.msn.it/results.aspx?q=' . $SearchTerm . '&format=rss&FORM=RSRE&count=' . $NumberItems . '&first=' . $showmin;
        }
        else
        {
          if ($lang == 'no')
          {
            $strQuery = '' . 'http://search.msn.no/results.aspx?q=' . $SearchTerm . '&format=rss&FORM=RSRE&count=' . $NumberItems . '&first=' . $showmin;
          }
          else
          {
            if ($lang == 'es')
            {
              $strQuery = '' . 'http://search.msn.es/results.aspx?q=' . $SearchTerm . '&format=rss&FORM=RSRE&count=' . $NumberItems . '&first=' . $showmin;
            }
            else
            {
              if ($lang == 'be')
              {
                $strQuery = '' . 'http://search.msn.be/results.aspx?q=' . $SearchTerm . '&format=rss&FORM=RSRE&count=' . $NumberItems . '&first=' . $showmin;
              }
              else
              {
                $strQuery = '' . 'http://search.msn.com/results.aspx?q=' . $SearchTerm . '&format=rss&FORM=RSRE&count=' . $NumberItems . '&first=' . $showmin;
              }
            }
          }
        }
      }
    }
 
    $xmlData = geturl ($strQuery);
    $xmlData = str_replace ('<strong>', '', $xmlData);
    $xmlData = str_replace ('</strong>', '', $xmlData);
    $xmlListingName = 'item';
    $xmlFields['Title'] = 'title';
    $xmlFields['Url'] = 'link';
    $xmlFields['Uri'] = 'link';
    $xmlFields['Description'] = 'description';
    $xmlFields['Bid'] = 'bid';
    parsexml ($xmlData);
    return $xmlItems;
  }
 
?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP search script, UK sites only.

Post by Benjamin »

MSN Search supports InUrl, so you should be able to appened InUrl:co.uk to your $SearchTerm variable.

http://blogs.msdn.com/livesearch/archiv ... 32439.aspx
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: PHP search script, UK sites only.

Post by tomsace »

Hi, sorry im quite new to PHP, could you help me as to where to integrate the InUrl:co.uk code?

Thanks alot,
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP search script, UK sites only.

Post by Benjamin »

Where you call your function would be a good place, or you could modify the function itself..

Code: Select all

 
$foo = getmsnarray ($SearchTerm . " inurl:co.uk", $var, $var, $var, $var, $var);
 
or on line 14 of the function, change it to:

Code: Select all

 
$SearchTerm = str_replace (' ', '+', $SearchTerm) . " inurl:co.uk";
 
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: PHP search script, UK sites only.

Post by tomsace »

Hi,
Thanks for the advice but I have tried both of your suggestions but I can't get neither to work. I have even tried both at the same time! :lol:

Do you have any other suggestions?

Tom.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP search script, UK sites only.

Post by Benjamin »

Not really, without knowing what is going on at least..
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: PHP search script, UK sites only.

Post by tomsace »

Hi,

I have found a website, mentions something to do with msn disabling codes such as inurl.
Here is the site:

http://www.naturalsearchblog.com/archiv ... o-for-seo/


Is there any other way of working this? How about at the end of every search made it inserts the word 'UK'?
how would I go about doing this?

Tom.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: PHP search script, UK sites only.

Post by mattpointblank »

At the start of the function you could do:

$searchTerm .= " UK";

which will add "UK" to the end.
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: PHP search script, UK sites only.

Post by tomsace »

Hey,

I have found that actually this script is loaded to call the search results:

Code: Select all

<?
 
 
  error_reporting (0);
  $search = str_replace ('\\', '', $search);
  $query = str_replace (' ', '+', $search);
  if ($theme == 'fr')
  {
    $request = '' . 'http://search.msn.fr/results.aspx?q=' . $query . '&format=rss&FORM=RSRE&count=' . $NumberItems . '&first=' . $showmin;
  }
  else
  {
    if ($theme == 'de')
    {
      $request = '' . 'http://search.msn.de/results.aspx?q=' . $query . '&format=rss&FORM=RSRE&count=' . $NumberItems . '&first=' . $showmin;
    }
    else
    {
      if ($theme == 'it')
      {
        $request = '' . 'http://search.msn.it/results.aspx?q=' . $query . '&format=rss&FORM=RSRE&count=' . $NumberItems . '&first=' . $showmin;
      }
      else
      {
        if ($theme == 'pt')
        {
          $request = '' . 'http://search.live.com/results.aspx?q=' . $query . '&first=' . $showmin . '&FORM=PERE&format=rss&mkt=pt-pt';
        }
        else
        {
          if ($theme == 'no')
          {
            $request = '' . 'http://search.msn.no/results.aspx?q=' . $query . '&format=rss&FORM=RSRE&count=' . $NumberItems . '&first=' . $showmin;
          }
          else
          {
            if ($theme == 'es')
            {
              $request = '' . 'http://search.msn.es/results.aspx?q=' . $query . '&format=rss&FORM=RSRE&count=' . $NumberItems . '&first=' . $showmin;
            }
            else
            {
              if ($theme == 'be')
              {
                $request = '' . 'http://search.msn.be/results.aspx?q=' . $query . '&format=rss&FORM=RSRE&count=' . $NumberItems . '&first=' . $showmin;
              }
              else
              {
                if ($theme == 'nz')
                {
                  $request = '' . 'http://search.msn.com/results.aspx?q=' . $query . '&format=rss&mkt=en-nz&FORM=RSRE&count=' . $NumberItems . '&first=' . $showmin;
                }
                else
                {
                  $request = '' . 'http://search.msn.com/results.aspx?q=' . $query . '&format=rss&mkt=en-US&FORM=RSRE&count=' . $NumberItems . '&first=' . $showmin;
                }
              }
            }
          }
        }
      }
    }
  }
 
  $session = curl_init ($request);
  curl_setopt ($session, CURLOPT_HEADER, true);
  curl_setopt ($session, CURLOPT_RETURNTRANSFER, true);
  $response = curl_exec ($session);
  curl_close ($session);
  $status_code = array ();
  preg_match ('/\\d\\d\\d/', $response, $status_code);
  switch ($status_code[0])
  {
    case 200:
    {
      break;
    }
 
    case 503:
    {
      exit ('Your call to Yahoo Web Services failed and returned an HTTP status of 503. That means: Service unavailable. An internal problem prevented us from returning data to you.');
      break;
    }
 
    case 403:
    {
      exit ('Your call to Yahoo Web Services failed and returned an HTTP status of 403. That means: Forbidden. You do not have permission to access this resource, or are over your rate limit.');
      break;
    }
 
    case 400:
    {
      exit ('Your call to Yahoo Web Services failed and returned an HTTP status of 400. That means:  Bad request. The parameters passed to the service did not match as expected. The exact error is returned in the XML response.');
      break;
    }
 
    default:
    {
      exit ('Your call to Yahoo Web Services returned an unexpected HTTP status of:' . $status_code[0]);
    }
  }
 
  if (!$xml = strstr ($response, '<?xml'))
  {
    $xml = null;
  }
 
  $filename1 = 'templates/' . $theme . '/external_msn_listing.txt';
  $handle = fopen ($filename1, 'rb');
  $SITETEMPLATE = fread ($handle, filesize ($filename1));
  fclose ($handle);
  list ($xml1, $xml2) = split ('</copyright>', $xml);
  if (preg_match ('/<totalResultsAvailable>(.*)<\\/totalResultsAvailable>/s', $xml, $matches))
  {
    $DATAHITS2 = $matches[1];
  }
 
  if (preg_match ('/<firstResultPosition>(.*)<\\/firstResultPosition>/s', $xml, $matches))
  {
    $startrank = $matches[1];
  }
 
  $pieces = explode ('</item>', $xml2);
  foreach ($pieces as $val)
  {
    if ($showhits != $Conf['listhits'])
    {
      $cutoffmsn = '' . $startrank;
    }
    else
    {
      $cutoffmsn = '' . $DATAHITS;
    }
 
    if ($cutoffmsn <= 9)
    {
      if (preg_match ('/<title>(.*)<\\/title>/s', $val, $matches))
      {
        $title = $matches[1];
      }
 
      if (preg_match ('/<description>(.*)<\\/description>/s', $val, $matches))
      {
        $description = $matches[1];
      }
 
      if (preg_match ('/<pubDate>(.*)<\\/pubDate>/s', $val, $matches))
      {
        $pubdate = $matches[1];
      }
 
      if (preg_match ('/<Destinations>(.*)<\\/Destinations>/s', $val, $matches))
      {
        $destinations = $matches[1];
      }
 
      if (preg_match ('/<UserPhotoURL>(.*)<\\/UserPhotoURL>/s', $val, $matches))
      {
        $photourl = $matches[1];
      }
 
      if (preg_match ('/<link>(.*)<\\/link>/s', $val, $matches))
      {
        $url = $matches[1];
      }
 
      if ($title != '')
      {
        ++$DATAHITS;
        ++$startrank;
        if ($showhits != $Conf['listhits'])
        {
          $ch = $showhits - $Conf['listhits'];
        }
        else
        {
          $ch = $showhits - $Conf['listhits'] + $forrage;
        }
 
        $rank = $ch + $startrank;
        $title = str_replace ('&apos;', '\'', $title);
        $description = str_replace ('&apos;', '\'', $description);
        $SITETEMPLATE2 = $SITETEMPLATE;
        $SITETEMPLATE2 = str_replace ('<module:photo>', $photourl, $SITETEMPLATE2);
        $SITETEMPLATE2 = str_replace ('<module:destinations>', $destinations, $SITETEMPLATE2);
        $SITETEMPLATE2 = str_replace ('<module:pubdate>', $pubdate, $SITETEMPLATE2);
        $SITETEMPLATE2 = str_replace ('<module:state>', $state, $SITETEMPLATE2);
        $SITETEMPLATE2 = str_replace ('<module:phone>', $phone, $SITETEMPLATE2);
        $SITETEMPLATE2 = str_replace ('<module:detailsurl>', $moreinfourl, $SITETEMPLATE2);
        $SITETEMPLATE2 = str_replace ('<module:mapurl>', $mapurl, $SITETEMPLATE2);
        $SITETEMPLATE2 = str_replace ('<module:distance>', $distance, $SITETEMPLATE2);
        if ($showthumbs == '1')
        {
          $SITETEMPLATE2 = str_replace ('<module:thumbshots>', '' . '<img src="http://open.thumbshots.org/image.pxf?url=' . $url . '" width=90 height=66>', $SITETEMPLATE2);
        }
 
        $SITETEMPLATE2 = str_replace ('<module:site>', $url, $SITETEMPLATE2);
        if ($url != '')
        {
          $SITETEMPLATE2 = str_replace ('<module:fullsite>', $url, $SITETEMPLATE2);
        }
        else
        {
          $SITETEMPLATE2 = str_replace ('<module:fullsite>', $moreinfourl, $SITETEMPLATE2);
        }
 
        $SITETEMPLATE2 = str_replace ('<module:num>', $num, $SITETEMPLATE2);
        $SITETEMPLATE2 = str_replace ('<module:cgiurl>', $Conf['cgiurl'], $SITETEMPLATE2);
        $SITETEMPLATE2 = str_replace ('<module:date>', $d1, $SITETEMPLATE2);
        $SITETEMPLATE2 = str_replace ('<module:rank>', $rank, $SITETEMPLATE2);
        $SITETEMPLATE2 = str_replace ('<module:hits>', $thehits, $SITETEMPLATE2);
        $SITETEMPLATE2 = str_replace ('<module:rating>', $rating, $SITETEMPLATE2);
        $SITETEMPLATE2 = str_replace ('<module:votes>', $votes, $SITETEMPLATE2);
        $SITETEMPLATE2 = str_replace ('<module:imageurl>', $Conf['imgdir'] . '/' . $theme, $SITETEMPLATE2);
        $SITETEMPLATE2 = str_replace ('<module:description>', $description, $SITETEMPLATE2);
        $SITETEMPLATE2 = str_replace ('<module:title>', $title, $SITETEMPLATE2);
        if ($url != '')
        {
          $SITETEMPLATE2 = str_replace ('<module:url>', $url, $SITETEMPLATE2);
        }
        else
        {
          $SITETEMPLATE2 = str_replace ('<module:url>', $moreinfourl, $SITETEMPLATE2);
        }
 
        $SITETEMPLATE2 = str_replace ('<module:new>', $newimage, $SITETEMPLATE2);
        $SITETEMPLATE2 = str_replace ('<module:imageurl>', $Conf['imgdir'], $SITETEMPLATE2);
        $feedmsn .= '' . $SITETEMPLATE2;
        continue;
      }
 
      continue;
    }
  }
 
?>
Any idea how to load just .co.uk sites from here?

Tom.
Post Reply