📜 ⬆️ ⬇️

Search on a site based on Yandex.XML

For some reason, webmasters are too lazy to do a normal search on their site. This is especially true of highly visited sites, where a high-quality search would be very convenient for the average user.

Most often resort to a ready-made solution from Google, with which you can also earn extra money on contextual advertising. But for Runet, I would advise you to do a search using the Yandex.XML service, because such a search is more adapted to the morphology of the Russian language. In addition, you can get a chance to get the fattest bids for context if you use the search direct.

In this post I want to show you in detail that organizing such a search is not at all difficult. It only takes a few minutes and will result in a dozen lines of PHP.

')
Step 1: Register IP on the Yandex service

To begin, go to http://xml.yandex.ru/ and register your IP:

image

Step 2: We read mana

The service is quite confusing navigation, so I immediately give a link to the documentation for the search query

Step 3: We write the program for PHP

Next, I bring in a part of my program, from which it will become clear how to create a similar module for my site.

Get the string to search using GET or POST

$q = $_GET['q'];

I run this variable through the regulars, so that all sorts of mischievous people would not think to shove anything there, taxes, for every fireman.

$q = preg_replace("/[^--\d\w\s]/iu", "", $q);

Yandex XML Search Server accepts search requests from the input stream using the POST method (in XML format). Also, requests are accepted by the GET method at xmlsearch.yandex.ru/xmlsearch. I didn’t go too far and decided to use the GET method. $ page is the page number, starting at 0, and yousite.ru is your site that will be searched.

$ya_query = "http://xmlsearch.yandex.ru/xmlsearch" . "/?page=" . $page . "&query=" . urlencode($q . " << host=\"yoursite.ru\"");

We send a GET request to Yasha:

$xml_data = file_get_contents($ya_query);

Then I removed part of the search query from the XML file (<< host = "yoursite.ru"), so that it "does not interfere under your feet."

$xml_data = str_replace("& lt ;& lt ; host=& quot ;yoursite.ru& quot ;", "", $xml_data);
(do not forget to remove spaces)

We now have an XML response. Now take the XSL template in order to insert our data there. Various templates can be found on the Yandex.xml service itself, and here I will give a link to my template , which you will have to customize. It seems I myself found it somewhere on the Internet, because it will be quite a lot of "garbage".

I hope that you will intuitively understand it. Principle, everything is quite simple.

We load our XSL template, I have smarty doing it, and you, let's say it will be like this:

$handle = fopen($filename, "r");
$xsl_data = fread($handle, filesize($filename));
fclose($handle);


Well, that's all, it remains only to use the power of PHP5 and put it all together:

$xh = new xsltprocessor();

$xml = new DOMDocument();
$xsl = new DOMDocument();

$xml->loadXML($xml_data);
$xsl->loadXML($xsl_data);

$xh->importStyleSheet($xsl);
$search_result = $xh->transformToXML($xml);


Well, I also changed the encoding to utf-8:

$search_result = iconv("windows-1251", "utf-8", $search_result);

We print our result:

print $search_result;

That's all science. An example of implementation can be found on my blog. As you can see it’s not so difficult to fasten a search on a site based on Yandex.XML!

I don't consider myself a programming guru, maybe I missed some details, or some places don't look very nice from a programmer's point of view. Please indicate to me my shortcomings, I will be very grateful.

Related topics:

PHP class for working with Yandex.XML

Source: https://habr.com/ru/post/76311/


All Articles