Google ile CURL kullanma

4 Cevap php

Ben belli bir arama için döner kaç sonuç görmek için Google Curl istiyorum.

Bu denedim:

  $url = "http://www.google.com/search?q=".$strSearch."&hl=en&start=0&sa=N";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_VERBOSE, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_POST, true);
  $response = curl_exec($ch);
  curl_close($ch);

Ama bu sadece bir 405 Yöntemi İzin google hata döndürür.

Herhangi bir fikir?

Teşekkürler

4 Cevap

GET isteği yerine POST isteği kullanın. Yani, kurtul

curl_setopt($ch, CURLOPT_POST, true);

Ya da daha iyisi, bunun yerine ekran kazıma kendi well defined search API kullanın.

Google Ajax API kullanın.

http://code.google.com/apis/ajaxsearch/

this thread sonuç sayısını almak için nasıl bakın. Bu c # kütüphaneleri ifade ederken, bu size bazı işaretçiler verebilir.

Google Hurdaya yapmak çok kolay bir şeydir. Eğer ilk 30 sonuç daha fazla ihtiyaç yoksa (diğerleri önerilen gibi) Ancak, daha sonra search API tercih edilir. Aksi takdirde, burada bazı örnek kod. Ben bu yüzden olduğu gibi tamamen işlevsel olmayabilir istimal sınıfların bir çiftin bu yırtık ettik, ama fikir almalısınız.

function queryToUrl($query, $start=null, $perPage=100, $country="US") {
    return "http://www.google.com/search?" . $this->_helpers->url->buildQuery(array(
        // Query
        "q"     => urlencode($query),
        // Country (geolocation presumably)
        "gl"    => $country,
        // Start offset
        "start" => $start,
        // Number of result to a page
        "num"   => $perPage
    ), true);
}

// Find first 100 result for "pizza" in Canada
$ch = curl_init(queryToUrl("pizza", 0, 100, "CA"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT,      $this->getUserAgent(/*$proxyIp*/));
curl_setopt($ch, CURLOPT_MAXREDIRS,      4);
curl_setopt($ch, CURLOPT_TIMEOUT,        5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

$response = curl_exec($ch);

Not: $this->_helpers->url->buildQuery() boş parametreleri düşecek dışında http_build_query ile aynıdır.

CURLOPT_CUSTOMREQUEST => ($ post)? "POST": "GET"