ultimix
yandex_xml.php
Go to the documentation of this file.
1 <?php
2 
3  /*
4  * This source code is a part of the Ultimix Project.
5  * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6  * it's own license wich could be found from the corresponding files or sources.
7  * This source code is provided "as is" without any warranties or garanties.
8  *
9  * Have a nice day!
10  *
11  * @url http://ultimix.sorceforge.net
12  *
13  * @author Alexey "gdever" Dodonov
14  */
15 
27 
38  var $Security = false;
39 
54  function __construct()
55  {
56  try
57  {
58  $this->Security = get_package( 'security' , 'last' , __FILE__ );
59  }
60  catch( Exception $e )
61  {
62  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
63  }
64  }
65 
76  var $Key = '';
77 
88  var $User = '/';
89 
108  function set( $FieldName , $FieldValue )
109  {
110  $this->$FieldName = $FieldValue;
111  }
112 
139  private function send_request( $Data , $Region = 0 )
140  {
141  try
142  {
143  $Header = "Content-type: application/xml\r\nContent-length: " . strlen( $Data );
144  $HttpDescription = array( 'method' => "POST" , 'content' => $Data , 'header' => $Header );
145  $ContextDescription = array( 'http' => $HttpDescription );
146  $Context = stream_context_create( $ContextDescription );
147 
148  $URL = "http://xmlsearch.yandex.ru/xmlsearch?user=".
149  "$this->User&key=$this->Key".( $Region ? "&lr=$Region" : '' );
150  $Result = file_get_contents( $URL , true , $Context );
151 
152  return( $Result );
153  }
154  catch( Exception $e )
155  {
156  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
157  }
158  }
159 
186  private function search_query( $Query , $Region = 0 )
187  {
188  try
189  {
190  $Data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
191  <request>
192  <query>$Query</query>
193  <groupings>
194  <groupby attr=\"d\" mode=\"deep\" groups-on-page=\"100\" docs-in-group=\"1\" />
195  </groupings>
196  </request>";
197 
198  return( $this->send_request( $Data , $Region ) );
199  }
200  catch( Exception $e )
201  {
202  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
203  }
204  }
205 
228  private function parse_response( $Response )
229  {
230  try
231  {
232  $XMLDoc = new SimpleXMLElement( $Response );
233 
234  return( $XMLDoc->xpath( "response/results/grouping/group/doc" ) );
235  }
236  catch( Exception $e )
237  {
238  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
239  }
240  }
241 
272  function get_position( $Domain , $Query , $Region = 0 )
273  {
274  try
275  {
276  $Domain = str_ireplace( 'http://' , '' , $Domain );
277  $Domain = rtrim( $Domain , '/\\' );
278 
279  $Response = $this->search_query( $Query , $Region );
280 
281  $Response = $this->parse_response( $Response );
282 
283  $Counter = 1;
284  foreach( $Response as $Item )
285  {
286  if( strtolower( $Item->domain ) == strtolower( $Domain ) )
287  {
288  return( $Counter );
289  }
290  $Counter++;
291  }
292 
293  return( false );
294  }
295  catch( Exception $e )
296  {
297  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
298  }
299  }
300 
331  function get_position_and_url( $Domain , $Query , $Region = 0 )
332  {
333  try
334  {
335  $Domain = str_ireplace( 'http://' , '' , $Domain );
336  $Domain = rtrim( $Domain , '/\\' );
337  $Response = $this->search_query( $Query , $Region );
338  $Response = $this->parse_response( $Response );
339 
340  if( empty( $Response ) )
341  {
342  return( array( false , false ) );
343  }
344 
345  $Counter = 1;
346  foreach( $Response as $Item )
347  {
348  if( strtolower( $Item->domain ) == strtolower( $Domain ) )
349  {
350  return( array( $Counter , "$Item->url" ) );
351  }
352  $Counter++;
353  }
354 
355  return( array( $Counter < count( $Response ) ? $Counter : 0 , 'undefined' ) );
356  }
357  catch( Exception $e )
358  {
359  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
360  }
361  }
362 
393  function get_url( $Domain , $Query , $Region = 0 )
394  {
395  try
396  {
397  $Domain = str_ireplace( 'http://' , '' , $Domain );
398  $Domain = rtrim( $Domain , '/\\' );
399 
400  $Response = $this->search_query( "$Query site:$Domain" , $Region );
401 
402  $Response = $this->parse_response( $Response );
403 
404  foreach( $Response as $Item )
405  {
406  if( strtolower( $Item->domain ) == strtolower( $Domain ) )
407  {
408  return( "$Item->url" );
409  }
410  }
411 
412  return( false );
413  }
414  catch( Exception $e )
415  {
416  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
417  }
418  }
419  }
420 
421  /*
422  $YandexXML = get_package( 'xml::yandex_xml' , 'last' , __FILE__ );
423 
424  $YandexXML->set( 'User' , 'gdever' );
425  $YandexXML->set( 'Key' , '03.47194176:143dc52413f92c472306881e7f75a626' );
426 
427  print( $YandexXML->get_position( 'gdzone.ru' , 'шанти' ) );
428 
429  exit( 0 );
430  */
431 
432 ?>