Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Sprawdzanie pozycji w google
Forum PHP.pl > Forum > Gotowe rozwiązania
ewaslawek5
Witam,

Umieszczam gotową klasę która przestała działać:
  1. <?php
  2. class checkPosition
  3. {
  4. const G_PATTERN = '/<h3 class="r"><a href="([^"]+)">/';
  5.  
  6. private $limit = 100;
  7. private $lang = 'pl';
  8. private $dc = 'www.google.pl';
  9. private $format = 'array'; //or 'json'
  10.  
  11. private $urls = array();
  12. private $phrases = array();
  13.  
  14. /*
  15. * Contains results
  16. */
  17. public $r = array();
  18.  
  19. public function setLimit( $limit ) {
  20. $this->limit = $limit;
  21. }
  22.  
  23. /*
  24. * Sets language (param hl=in google query)
  25. */
  26. public function setLang( $lang ) {
  27. $this->lang = $lang;
  28. }
  29.  
  30. /*
  31. * Sets DC (it can be url or IP)
  32. */
  33. public function setDC( $dc ) {
  34. $this->dc = $dc;
  35. }
  36.  
  37. /*
  38. * Sets returned format
  39. */
  40. public function setFormat( $format ) {
  41. $this->format = $format;
  42. }
  43.  
  44. public function getRank() {
  45. if( count( $this->urls ) > 1 && count( $this->phrases ) == 1 )
  46. self::checkManyURLs();
  47. elseif( count( $this->phrases ) > 1 && count( $this->urls ) == 1 )
  48. self::checkManyPhrases();
  49. else
  50. die('Wrong params!');
  51. }
  52.  
  53. /*
  54. * gets host from URL
  55. * @param $page (String) - url e.g. <a href="http://example.com" target="_blank">http://example.com</a>
  56. * @return (String) - host e.g. "example.com"
  57. */
  58. private function getHost( $page )
  59. {
  60. preg_match('@^(?:http://)?([^/]+)@i', $page, $matches);
  61. $matches[1] = str_replace("www.", "", $matches[1]);
  62. return $matches[1];
  63. }
  64.  
  65. /*
  66. * Send request to Google server
  67. * @param $url (String) - google query url
  68. * @return (Array) - array with results (urls / serp)
  69. */
  70. private function sendRequest( $url ) {
  71. $c = curl_init();
  72. curl_setopt( $c, CURLOPT_HEADER, 0 );
  73. curl_setopt( $c, CURLOPT_RETURNTRANSFER, 1 );
  74. curl_setopt( $c, CURLOPT_VERBOSE, 1 );
  75. curl_setopt( $c, CURLOPT_REFERER, $this->dc );
  76. curl_setopt( $c, CURLOPT_URL, $url );
  77. $d = curl_exec( $c );
  78. curl_close( $c );
  79.  
  80. preg_match_all( self::G_PATTERN, $d, $r );
  81. $r = array_pop( $r );
  82.  
  83. return $r;
  84. }
  85.  
  86. private function checkManyURLs() {
  87. $phrase = urlencode( $this->phrases[0] );
  88. $url = 'http://'. $this->dc .'/search?hl='. $this->lang .'&q='. $phrase .'&num='. $this->limit;
  89. $this->r['phrase'] = $this->phrases[0];
  90.  
  91. $r = self::sendRequest( $url );
  92.  
  93. for( $i=0, $il=count( $this->urls ); $i<$il; $i++ ) {
  94. $ii = 0;
  95.  
  96. $this->r[ $i ]['url'] = $this->urls[ $i ];
  97. $this->r[ $i ]['url_host'] = self::getHost( $this->urls[ $i ] );
  98. $this->r[ $i ]['position'] = 0;
  99.  
  100. for( $p=0; $p<$this->limit; $p++ ) {
  101. $sPage = self::getHost($r[$p]);
  102.  
  103. if( $r[($p+1)] ) {
  104. $sNext = self::getHost($r[($p+1)]);
  105. if( $sDomena != $sNext ) {
  106. $ii++;
  107. if( $sPage == $this->r[ $i ]['url_host'] )
  108. $this->r[ $i ]['position'] = $ii;
  109. }
  110. $sDomena = $sNext;
  111. }
  112. }
  113. }
  114. }
  115.  
  116. private function checkManyPhrases() {
  117. $this->r['url'] = $this->urls[ 0 ];
  118. $this->r['url_host'] = self::getHost( $this->urls[ 0 ] );
  119.  
  120. for( $i=0, $il=count( $this->phrases ); $i<$il; $i++ ) {
  121. $phrase = urlencode( $this->phrases[0] );
  122. $url = 'http://'. $this->dc .'/search?hl='. $this->lang .'&q='. $this->phrases[ $i ] .'&num='. $this->limit;
  123. $ii = 0;
  124. $r = self::sendRequest( $url );
  125.  
  126. $this->r[ $i ]['phrase'] = $this->phrases[ $i ];
  127. $this->r[ $i ]['position'] = 0;
  128.  
  129. for( $p=0; $p<$this->limit; $p++ ) {
  130. $sPage = self::getHost($r[$p]);
  131.  
  132. if( $r[($p+1)] ) {
  133. $sNext = self::getHost($r[($p+1)]);
  134. if( $sDomena != $sNext ) {
  135. $ii++;
  136. if( $sPage == $this->r['url_host'] ) {
  137. $this->r[ $i ]['position'] = $ii;
  138. break;
  139. }
  140. }
  141. $sDomena = $sNext;
  142. }
  143. }
  144. }
  145. }
  146.  
  147. public function getResults() {
  148. switch( $this->format ) {
  149. case 'array' :
  150. return $this->r;
  151.  
  152. case 'json' :
  153. return json_encode( $this->r );
  154. }
  155. }
  156. }
  157. //-------------------------------------------------------
  158. $c = new checkPosition( array( 'http://www.wp.pl' ), array( 'wirtualna polska', 'wydarzenia', 'aktualnosci', 'prognoza pogody', 'wiadomosci z kraju' ) );
  159.  
  160. $c->getRank();
  161.  
  162. $wynik = $c->getResults();
  163.  
  164. print_r ($wynik);


Klasa przestała działać exclamation.gif!

Wiem że google zmienia co jakiś czas swoje struktury, żeby tego typu skrypty przestały działać.

Mógł by ktoś mnie naprowadzić na co mam zwrócić szczególną uwagę i naprowadzić mnie żebym mógł zmodyfikować żeby znów działało smile.gif
Na pewno powyższa klasa przydała by się wielu osobą smile.gif
annat
Udało Ci się może rozwiązać ten problem? Z góry dzięki za pomoc.
marekge
A czy czasem google nie zablokowało Ci adresu IP ze względu na częste i głębokie odpytywanie pozycji?
annat
Nie, skrypt przestał działać na rożnych serwerach...
Helid
http://shpyo.net/php/google-rank/ - spróbuj tej, jak ona zawodzi to coś się zmieniło w strukturze.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2024 Invision Power Services, Inc.