Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Problem z skryptem
Forum PHP.pl > Forum > Przedszkole
szczalpi
Witam
Mam skrypt, ale nie chce zaskoczyć. Nie wiem gdzie mam błąd.
Oto on:
  1. <form method="get" action="index.php">
  2. <p style="border-bottom: 2px solid rgb(197, 227, 248); font-size: 14px; text-align: center;">Adres strony:</p><br/>
  3. <input style="border: 2px solid rgb(197, 227, 248); padding: 4px;width: 200px;" type="text" name="adres"/><br/>
  4. <p style="border-bottom: 2px solid rgb(197, 227, 248); font-size: 14px;text-align: center;">Fraza:</p><br/>
  5. <input style="border: 2px solid rgb(197, 227, 248); padding: 4px;width: 200px;" type="text" name="fraza"/><br/><br/>
  6. <input style="border: 2px solid rgb(197, 227, 248); padding: 4px;" type="submit" value="Sprawdź pozycje"/></form></div>
  7. <?php
  8.  
  9.  
  10. $urls = $_GET['adres'];
  11. $phrases = $_GET['fraza'];
  12.  
  13. $c = new checkPosition( $urls, $phrases );
  14.  
  15.  
  16. class checkPosition
  17. {
  18. const G_PATTERN = '/<h3 class="r"><a href="([^"]+)">/';
  19.  
  20. private $limit = 100;
  21. private $lang = 'pl';
  22. private $dc = 'www.google.pl';
  23. private $format = 'array'; //or 'json'
  24.  
  25. private $urls = array();
  26. private $phrases = array();
  27.  
  28. /*
  29. * Contains results
  30. */
  31. public $r = array();
  32.  
  33. /*
  34. * Constructor
  35. * @param $url (Mixed) - array with URLs
  36. * @param $phrase (Mixed) - array with phrases
  37. * $url = array( 'http://example.com' ) or
  38. * $url = array( 'http://example.com', 'http://example2.com', ... );
  39. * $phrase = array( 'phrase1' ) or
  40. * $phrase = array( 'phrase1', 'phrase2', 'keyword3' )
  41. */
  42. public function __construct( $url, $phrase ) {
  43. $this->urls = $url;
  44. $this->phrases = $phrase;
  45. }
  46.  
  47. /*
  48. * Sets limit (max results to check)
  49. */
  50. public function setLimit( $limit ) {
  51. $this->limit = $limit;
  52. }
  53.  
  54. /*
  55. * Sets language (param hl=in google query)
  56. */
  57. public function setLang( $lang ) {
  58. $this->lang = $lang;
  59. }
  60.  
  61. /*
  62. * Sets DC (it can be url or IP)
  63. */
  64. public function setDC( $dc ) {
  65. $this->dc = $dc;
  66. }
  67.  
  68. /*
  69. * Sets returned format
  70. */
  71. public function setFormat( $format ) {
  72. $this->format = $format;
  73. }
  74.  
  75. public function getRank() {
  76. if( count( $this->urls ) > 1 && count( $this->phrases ) == 1 )
  77. self::checkManyURLs();
  78. elseif( count( $this->phrases ) > 1 && count( $this->urls ) == 1 )
  79. self::checkManyPhrases();
  80. else
  81. die('Wrong params!');
  82. }
  83.  
  84. /*
  85. * gets host from URL
  86. * @param $page (String) - url e.g. <a href="http://example.com" target="_blank">http://example.com</a>
  87. * @return (String) - host e.g. "example.com"
  88. */
  89. private function getHost( $page )
  90. {
  91. preg_match('@^(?:http://)?([^/]+)@i', $page, $matches);
  92. $matches[1] = str_replace("www.", "", $matches[1]);
  93. return $matches[1];
  94. }
  95.  
  96. /*
  97. * Send request to Google server
  98. * @param $url (String) - google query url
  99. * @return (Array) - array with results (urls / serp)
  100. */
  101. private function sendRequest( $url ) {
  102. $c = curl_init();
  103. curl_setopt( $c, CURLOPT_HEADER, 0 );
  104. curl_setopt( $c, CURLOPT_RETURNTRANSFER, 1 );
  105. curl_setopt( $c, CURLOPT_VERBOSE, 1 );
  106. curl_setopt( $c, CURLOPT_REFERER, $this->dc );
  107. curl_setopt( $c, CURLOPT_URL, $url );
  108. $d = curl_exec( $c );
  109. curl_close( $c );
  110.  
  111. preg_match_all( self::G_PATTERN, $d, $r );
  112. $r = array_pop( $r );
  113.  
  114. return $r;
  115. }
  116.  
  117. private function checkManyURLs() {
  118. $phrase = urlencode( $this->phrases[0] );
  119. $url = 'http://'. $this->dc .'/search?hl='. $this->lang .'&q='. $phrase .'&num='. $this->limit;
  120. $this->r['phrase'] = $this->phrases[0];
  121.  
  122. $r = self::sendRequest( $url );
  123.  
  124. for( $i=0, $il=count( $this->urls ); $i<$il; $i++ ) {
  125. $ii = 0;
  126.  
  127. $this->r[ $i ]['url'] = $this->urls[ $i ];
  128. $this->r[ $i ]['url_host'] = self::getHost( $this->urls[ $i ] );
  129. $this->r[ $i ]['position'] = 0;
  130.  
  131. for( $p=0; $p<$this->limit; $p++ ) {
  132. $sPage = self::getHost($r[$p]);
  133.  
  134. if( $r[($p+1)] ) {
  135. $sNext = self::getHost($r[($p+1)]);
  136. if( $sDomena != $sNext ) {
  137. $ii++;
  138. if( $sPage == $this->r[ $i ]['url_host'] )
  139. $this->r[ $i ]['position'] = $ii;
  140. }
  141. $sDomena = $sNext;
  142. }
  143. }
  144. }
  145. }
  146.  
  147. private function checkManyPhrases() {
  148. $this->r['url'] = $this->urls[ 0 ];
  149. $this->r['url_host'] = self::getHost( $this->urls[ 0 ] );
  150.  
  151. for( $i=0, $il=count( $this->phrases ); $i<$il; $i++ ) {
  152. $phrase = urlencode( $this->phrases[0] );
  153. $url = 'http://'. $this->dc .'/search?hl='. $this->lang .'&q='. $this->phrases[ $i ] .'&num='. $this->limit;
  154. $ii = 0;
  155. $r = self::sendRequest( $url );
  156.  
  157. $this->r[ $i ]['phrase'] = $this->phrases[ $i ];
  158. $this->r[ $i ]['position'] = 0;
  159.  
  160. for( $p=0; $p<$this->limit; $p++ ) {
  161. $sPage = self::getHost($r[$p]);
  162.  
  163. if( $r[($p+1)] ) {
  164. $sNext = self::getHost($r[($p+1)]);
  165. if( $sDomena != $sNext ) {
  166. $ii++;
  167. if( $sPage == $this->r['url_host'] ) {
  168. $this->r[ $i ]['position'] = $ii;
  169. break;
  170. }
  171. }
  172. $sDomena = $sNext;
  173. }
  174. }
  175. }
  176. }
  177.  
  178. public function getResults() {
  179. switch( $this->format ) {
  180. case 'array' :
  181. return $this->r;
  182.  
  183. case 'json' :
  184. return json_encode( $this->r );
  185. }
  186. }
  187. }
  188. $c->getRank();
  189.  
  190. ?>
  191.  
  192. <? //echo $c->getRank();?>
  193. <?//echo $c->getResults();?>
  194. <p style="font-size:19px;text-align: center;"><?echo $urls;?> : <?echo $c->getResults();?></p>

Proszę o pomoc.
Pozdrawiam
Fifi209
Zapomniałeś napisać w czym jest błąd, rzuciłeś kawał kodu nie pisząc nawet skąd go masz i za co odpowiada.
szczalpi
Skrypt do sprawdzania pozycji. Skrypt jest shpyo. Chce zrobic sobie sprawdzanie pozycji swoich stron, lecz nie potrafie go dobrze skonfigurowac.
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-2025 Invision Power Services, Inc.