Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP][Regexy][cURL]Pobieranie wyników z Google
infoo1
post
Post #1





Grupa: Zarejestrowani
Postów: 117
Pomógł: 0
Dołączył: 12.11.2008

Ostrzeżenie: (0%)
-----


Mam taki kod:
  1. <?php
  2.    error_reporting(E_ALL);
  3.    ini_set('display_errors',1);
  4.    class google
  5.    {
  6.        public $word;
  7.        public $results    =    1000;
  8.  
  9.        public function get_results_num()
  10.        {
  11.            if(empty($this->word))
  12.            {
  13.                return false;
  14.            }
  15.  
  16.            if (strpos($this->word, "."))
  17.            {
  18.                $this->word    =    substr($this->word, 0, strpos($this->word, "."));
  19.            }
  20.  
  21.            $url = "http://www.google.com/search?num=1&q=".trim(urlencode($this->word));
  22.            $curl = curl_init($url);
  23.  
  24.            curl_setopt($curl, CURLOPT_HEADER, false);
  25.            curl_setopt($curl, CURLOPT_HTTPGET, true);
  26.            curl_setopt($curl, CURLOPT_POST, false);
  27.            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  28.  
  29.  
  30.            $this->curl_result = curl_exec($curl);
  31.            curl_close($curl);
  32.  
  33.            if (preg_match('/(of about <b>([0-9,]+)</b>)/', $this->curl_result, $out))
  34.            {
  35.                $res = $out[2];
  36.                return preg_replace('/,/', '', $res);
  37.            }
  38.            return false;
  39.        }
  40.  
  41.        public function get_results()
  42.        {
  43.            $url = "http://www.google.com/search?num=100&q=".trim(urlencode($this->word));
  44.            $curl = curl_init($url);
  45.  
  46.            curl_setopt($curl, CURLOPT_HEADER, false);
  47.            curl_setopt($curl, CURLOPT_HTTPGET, true);
  48.            curl_setopt($curl, CURLOPT_POST, false);
  49.            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  50.  
  51.  
  52.            $this->curl_result = curl_exec($curl);
  53.            curl_close($curl);
  54.  
  55.            if (preg_match('#<!--m-->(.*?)<h3 class=r><a href="(.*?)" class=1 onmousedown=(.*?)<!--n-->#', $this->curl_result, $out))
  56.            {
  57.                $res = $out[1];
  58.                return preg_split('#<!--m-->(.*?)<h3 class=r><a href="(.*?)" class=1 onmousedown=(.*?)<!--n-->#',$res);
  59.            }
  60.        }
  61.    }
  62.  
  63.    $google            =    new google;
  64.    $google->word    =    $_GET['q'];
  65.    echo $google->get_results_num();
  66.    print_r($google->get_results());
  67.    die;
  68. ?>
Pierwsza funkcja działa dobrze, ale druga - nie. W ogóle nie wchodzi w if-a. Co zrobić, aby ta druga (get_results()) zwróciła mi taką tablicę dla zapytania "a":
  1. <?php
  2.  0 => 'http://pl.wikipedia.org/wiki/A',
  3. 1 => 'http://pl.wikipedia.org/wiki/Witamina_A',
  4. 2 => 'http://a.pl/',
  5. 3 => 'http://www.wrower.pl/',
  6.  4 => 'http://home.agh.edu.pl/polak/slownik/'
  7. )
  8. ?>
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 20.12.2025 - 23:58