Witam
Mam skrypt, ale nie chce zaskoczyć. Nie wiem gdzie mam błąd.
Oto on:
<form method="get" action="index.php">
<p style="border-bottom: 2px solid rgb(197, 227, 248); font-size: 14px; text-align: center;">Adres strony:</p><br/>
<input style="border: 2px solid rgb(197, 227, 248); padding: 4px;width: 200px;" type="text" name="adres"/><br/>
<p style="border-bottom: 2px solid rgb(197, 227, 248); font-size: 14px;text-align: center;">Fraza:</p><br/>
<input style="border: 2px solid rgb(197, 227, 248); padding: 4px;width: 200px;" type="text" name="fraza"/><br/><br/>
<input style="border: 2px solid rgb(197, 227, 248); padding: 4px;" type="submit" value="Sprawdź pozycje"/></form></div>
<?php
$urls = $_GET['adres'];
$phrases = $_GET['fraza'];
$c = new checkPosition( $urls, $phrases );
class checkPosition
{
const G_PATTERN = '/<h3 class="r"><a href="([^"]+)">/';
private $limit = 100;
private $lang = 'pl';
private $dc = 'www.google.pl';
private $format = 'array'; //or 'json'
private $phrases = array();
/*
* Contains results
*/
/*
* Constructor
* @param $url (Mixed) - array with URLs
* @param $phrase (Mixed) - array with phrases
* $url = array( 'http://example.com' ) or
* $url = array( 'http://example.com', 'http://example2.com', ... );
* $phrase = array( 'phrase1' ) or
* $phrase = array( 'phrase1', 'phrase2', 'keyword3' )
*/
public function __construct( $url, $phrase ) {
$this->urls = $url;
$this->phrases = $phrase;
}
/*
* Sets limit (max results to check)
*/
public function setLimit( $limit ) {
$this->limit = $limit;
}
/*
* Sets language (param hl=in google query)
*/
public function setLang( $lang ) {
$this->lang = $lang;
}
/*
* Sets DC (it can be url or IP)
*/
public function setDC( $dc ) {
$this->dc = $dc;
}
/*
* Sets returned format
*/
public function setFormat( $format ) {
$this->format = $format;
}
public function getRank() {
if( count( $this->urls ) > 1
&& count( $this->phrases ) == 1
) self::checkManyURLs();
elseif( count( $this->phrases ) > 1
&& count( $this->urls ) == 1
) self::checkManyPhrases();
else
}
/*
* gets host from URL
* @param $page (String) - url e.g. <a href="http://example.com" target="_blank">http://example.com</a>
* @return (String) - host e.g. "example.com"
*/
private function getHost( $page )
{
preg_match('@^(?:http://)?([^/]+)@i', $page, $matches); return $matches[1];
}
/*
* Send request to Google server
* @param $url (String) - google query url
* @return (Array) - array with results (urls / serp)
*/
private function sendRequest( $url ) {
$c = curl_init();
curl_setopt( $c, CURLOPT_HEADER, 0 );
curl_setopt( $c, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $c, CURLOPT_VERBOSE, 1 );
curl_setopt( $c, CURLOPT_REFERER, $this->dc );
curl_setopt( $c, CURLOPT_URL, $url );
$d = curl_exec( $c );
curl_close( $c );
return $r;
}
private function checkManyURLs() {
$url = 'http://'. $this->dc .'/search?hl='. $this->lang .'&q='. $phrase .'&num='. $this->limit;
$this->r['phrase'] = $this->phrases[0];
$r = self::sendRequest( $url );
for( $i=0
, $il=count( $this->urls ); $i<$il; $i++ ) { $ii = 0;
$this->r[ $i ]['url'] = $this->urls[ $i ];
$this->r[ $i ]['url_host'] = self::getHost( $this->urls[ $i ] );
$this->r[ $i ]['position'] = 0;
for( $p=0; $p<$this->limit; $p++ ) {
$sPage = self::getHost($r[$p]);
if( $r[($p+1)] ) {
$sNext = self::getHost($r[($p+1)]);
if( $sDomena != $sNext ) {
$ii++;
if( $sPage == $this->r[ $i ]['url_host'] )
$this->r[ $i ]['position'] = $ii;
}
$sDomena = $sNext;
}
}
}
}
private function checkManyPhrases() {
$this->r['url'] = $this->urls[ 0 ];
$this->r['url_host'] = self::getHost( $this->urls[ 0 ] );
for( $i=0
, $il=count( $this->phrases ); $i<$il; $i++ ) { $url = 'http://'. $this->dc .'/search?hl='. $this->lang .'&q='. $this->phrases[ $i ] .'&num='. $this->limit;
$ii = 0;
$r = self::sendRequest( $url );
$this->r[ $i ]['phrase'] = $this->phrases[ $i ];
$this->r[ $i ]['position'] = 0;
for( $p=0; $p<$this->limit; $p++ ) {
$sPage = self::getHost($r[$p]);
if( $r[($p+1)] ) {
$sNext = self::getHost($r[($p+1)]);
if( $sDomena != $sNext ) {
$ii++;
if( $sPage == $this->r['url_host'] ) {
$this->r[ $i ]['position'] = $ii;
break;
}
}
$sDomena = $sNext;
}
}
}
}
public function getResults() {
switch( $this->format ) {
case 'array' :
return $this->r;
case 'json' :
return json_encode( $this->r );
}
}
}
$c->getRank();
?>
<? //echo $c->getRank();?>
<?//echo $c->getResults();?>
<p style
="font-size:19px;text-align: center;"><?echo $urls;?> :
<?echo $c->getResults();?></p>
Proszę o pomoc.
Pozdrawiam