Search code examples
phpgoogle-search-api

Persian google search api in PHP


I am using Google web search API in my PHP(laravel) app for search a Persian(farsi) word in web ...

I am using this code for this :

<?php

$url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=فارسی&userip=192.168.106.1";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://cafesearch.ir/');
$body = curl_exec($ch);
curl_close($ch);

$json = json_decode($body);
var_dump($json->responseData);

there are not any problems with English , but in Persian I have not any results...

How I can solve this problem?


Solution

  • You need to encode the Persian bit of your url using php's urlencode method.

    So basically change your url's definition as the following.

    $keyword = urlencode('فارسی');    
    $url     = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=$keyword&userip=192.168.106.1";
    

    using this will give me the following output, which is matching with the result you may get when visiting the original url in your browser

    class stdClass#2 (2) {
      public $results =>
      array(4) {
        [0] =>
        class stdClass#3 (8) {
          public $GsearchResultClass =>
          string(10) "GwebSearch"
          public $unescapedUrl =>
          string(46) "https://en.wikipedia.org/wiki/Persian_language"
          public $url =>
          string(46) "https://en.wikipedia.org/wiki/Persian_language"
          public $visibleUrl =>
          string(16) "en.wikipedia.org"
          public $cacheUrl =>
          string(66) "http://www.google.com/search?q=cache:xHz0PgdVXjEJ:en.wikipedia.org"
          public $title =>
          string(51) "Persian language - Wikipedia, the free encyclopedia"
          public $titleNoFormatting =>
          string(51) "Persian language - Wikipedia, the free encyclopedia"
          public $content =>
          string(201) "Persian (/ˈpɜrʒən/ or /ˈpɜrʃən/), also known by its endonym <b>Farsi</b> or Parsi (
    English: /ˈfɑrsiː/; Persian: <b>فارسی</b> fārsi [fɒːɾˈsiː] ( listen)), is the 
    predominant ..."
        }
        [1] =>
        class stdClass#4 (8) {
          public $GsearchResultClass =>
          string(10) "GwebSearch"
          public $unescapedUrl =>
          string(85) "https://fa.wikipedia.org/wiki/%D8%B2%D8%A8%D8%A7%D9%86_%D9%81%D8%A7%D8%B1%D8%B3%DB%8C"
          public $url =>
          string(121) "https://fa.wikipedia.org/wiki/%25D8%25B2%25D8%25A8%25D8%25A7%25D9%2586_%25D9%2581%25D8%25A7%25D8%25B1%25D8%25B3%25DB%258C"
          public $visibleUrl =>
          string(16) "fa.wikipedia.org"
          public $cacheUrl =>
          string(66) "http://www.google.com/search?q=cache:Wa73LPvsgnQJ:fa.wikipedia.org"
          public $title =>
          string(78) "زبان <b>فارسی</b> - ویکی‌پدیا، دانشنامهٔ آزاد"
          public $titleNoFormatting =>
          string(71) "زبان فارسی - ویکی‌پدیا، دانشنامهٔ آزاد"
          public $content =>
          string(310) "<b>فارسی</b> یکی از زبان‌های هندواروپایی در شاخهٔ زبان‌های ایرانی جنوب غربی است که در 
    کشورهای ایران، افغانستان، تاجیکستان و ازبکستان به آن سخن می‌گویند. <b>فارسی</b> 
    زبان ..."
        }
        [2] =>
        class stdClass#5 (8) {
          public $GsearchResultClass =>
          string(10) "GwebSearch"
          public $unescapedUrl =>
          string(70) "http://www.bbc.com/persian/tvandradio/2013/08/000001_bbcpersian_livetv"
          public $url =>
          string(70) "http://www.bbc.com/persian/tvandradio/2013/08/000001_bbcpersian_livetv"
          public $visibleUrl =>
          string(11) "www.bbc.com"
          public $cacheUrl =>
          string(61) "http://www.google.com/search?q=cache:HsItkryX0ZAJ:www.bbc.com"
          public $title =>
          string(101) "تلویزیون <b>فارسی</b> بی‌بی‌سی: پخش زنده اینترنتی - BBC Persian"
          public $titleNoFormatting =>
          string(94) "تلویزیون فارسی بی‌بی‌سی: پخش زنده اینترنتی - BBC Persian"
          public $content =>
          string(307) "برنامه های تلویزیون <b>فارسی</b>، هر روز به طور مستقیم و زنده از وبسایت <b>فارسی</b> 
    بی‌بی‌سی نیزپخش می شود. با این حال، امکان تماشای بخشی از برنامه‌های تلویزیونی 
    به دلیل ..."
        }
        [3] =>
        class stdClass#6 (8) {
          public $GsearchResultClass =>
          string(10) "GwebSearch"
          public $unescapedUrl =>
          string(30) "http://www.farsinet.com/farsi/"
          public $url =>
          string(30) "http://www.farsinet.com/farsi/"
          public $visibleUrl =>
          string(16) "www.farsinet.com"
          public $cacheUrl =>
          string(66) "http://www.google.com/search?q=cache:yI1zxZstf_oJ:www.farsinet.com"
          public $title =>
          string(91) "<b>Farsi</b>, the most widely spoken Persian Language, a <b>Farsi</b> Dictionary <b>...</b>"
          public $titleNoFormatting =>
          string(70) "Farsi, the most widely spoken Persian Language, a Farsi Dictionary ..."
          public $content =>
          string(156) "Persian Language, also known as <b>Farsi</b>, is the most widely spoken member of 
    the Iranian branch of the Indo-Iranian languages, a subfamily of the ..."
        }
      }
      public $cursor =>
      class stdClass#7 (6) {
        public $resultCount =>
        string(10) "56,600,000"
        public $pages =>
        array(8) {
          [0] =>
          class stdClass#8 (2) {
            ...
          }
          [1] =>
          class stdClass#9 (2) {
            ...
          }
          [2] =>
          class stdClass#10 (2) {
            ...
          }
          [3] =>
          class stdClass#11 (2) {
            ...
          }
          [4] =>
          class stdClass#12 (2) {
            ...
          }
          [5] =>
          class stdClass#13 (2) {
            ...
          }
          [6] =>
          class stdClass#14 (2) {
            ...
          }
          [7] =>
          class stdClass#15 (2) {
            ...
          }
        }
        public $estimatedResultCount =>
        string(8) "56600000"
        public $currentPageIndex =>
        int(0)
        public $moreResultsUrl =>
        string(102) "http://www.google.com/search?oe=utf8&ie=utf8&source=uds&start=0&hl=en&q=%D9%81%D8%A7%D8%B1%D8%B3%DB%8C"
        public $searchResultTime =>
        string(4) "0.36"
      }
    }