📜 ⬆️ ⬇️

PHP class for working with Yandex.XML



The Yandex.XML service is already available for a long time - this is nothing but a search service that allows you to do automatic search queries to Yandex and publish its answers on your website and this post is dedicated to taming this beast using PHP.

! : This post is the development of dialogue in the topic habrahabr.ru/blogs/php/36932

First we need to register the site’s IP address in the Yandex database. After that, we will have access to a free search option, which involves no more than 1000 requests per day from a single IP. Also, one of the conditions for using the service is to place the phrase “Search is implemented based on Yandex.XML” on the search results page.
')
Now let's start PHP directly, we need the Yandex class (a modest name - they may not understand), you can download it from the project page on Code-Google , you can also access the SVN repository:

svn checkout yandex.googlecode.com/svn/trunk yandex-read-only


The requirements are quite acceptable:

Let's start, first we connect the class itself:

require_once 'Yandex.php';


Next, we need to retrieve the search request from the request, the page number and the site we will search for (it is likely that you will need many more parameters, but we will consider this a necessary minimum):

// "query", "page" "host" request'
$query = isset($_REQUEST['query'])?$_REQUEST['query']:'';
$page = isset($_REQUEST['page']) ?$_REQUEST['page']:0;
$host = isset($_REQUEST['host']) ?$_REQUEST['host']:null;


Now directly manipulate Yandex class:

 if ($ query) {
     // create an instance of the Yandex class
     $ Yandex = new Yandex ();
    
     $ Yandex -> query ($ query) // set the search query 
             -> host ($ host) // limit to site search
             -> page ($ page) // current page
             -> limit (10) // results per page
             -> set ('max-title-length', 160) // fine tuning of the search results (see http://code.google.com/p/yandex/source/browse/trunk/Yandex.php#48)
             -> set ('max-passage-length', 200)
             -> request () // send request
             ;
 }


If everything went smoothly, the result will be available in "$ Yandex-> ​​result" - this is a SimpleXML object, and nothing is cut into anything - in order to provide complete freedom of action for its processing. Next, I give an example of displaying the search results (HTML is omitted):

 // check if everything is smooth
 if (isset ($ Yandex) && empty ($ Yandex-> ​​error)): 

     // this is how we knock on the results
     foreach ($ Yandex-> ​​result-> response-> results-> grouping-> group as $ group):
         // display URL
         echo $ group-> doc-> url;
         // header output - Yandex :: highlight method selects the search phrase
         Yandex :: highlight ($ group-> doc-> title);
         // display passages
         foreach ($ group-> doc-> passages-> passage as $ passage):
              Yandex :: highlight ($ passage);                 
         endforeach;
     endforeach;

     // next we display page navigation, it is a bit cumbersome
     foreach ($ Yandex-> ​​pageBar () as $ page => $ value):
                 // switch statement for $ value ['type']
                 switch ($ value ['type']) {
                       // link to the page
                	 case 'link':
                		 echo '<a href="'. $url .'&page=' $ $ .'" title="Page'. ($page+1) .'">'.  sprintf ($ value ['text'], $ page + 1). '</a> |  ';
                		 break;
                       // current page
                	 case 'current':
                		 echo sprintf ($ value ['text'], $ page + 1). '  |  ';
                		 break;
                       // delimiter text - ".."
                	 case 'text':
                		 echo $ value ['text']. '  |  ';
                		 break;
                
                	 default:
                		 break;
                 }
     endforeach;

 // if something is wrong - we display an error
 elseif (isset ($ Yandex) && isset ($ Yandex-> ​​error)):
     echo $ Yandex-> ​​error;
 endif;


In this example, constructions are used in the form “if (..): ... endif;”, since they are most suitable for template engines with native PHP syntax, to wrap this in Smarty, you need to hide $ Yandex in the template and then rework the example:

// - :
$Smarty->assign("Yandex", $Yandex);


I think it’s not worthwhile to disassemble the class itself in terms of spare parts - who knows PHP already understands - there are no comments, and there is nothing military about it - everything is quite simple. If there are wishes or comments - write - we will discuss ...

You can taste the script on the page http://yandex.hohli.com/

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


All Articles