波斯语 google 在 PHP 中搜索 api
Persian google search api in PHP
我在 PHP(laravel) 应用程序中使用 Google web search API 在网络中搜索波斯语(波斯语)单词 ...
我为此使用此代码:
<?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);
英语没有任何问题,但波斯语我没有任何结果...
我该如何解决这个问题?
您需要使用 php 的 urlencode 方法对 url 的波斯语位进行编码。
所以基本上改变你的url的定义如下。
$keyword = urlencode('فارسی');
$url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=$keyword&userip=192.168.106.1";
使用它会得到以下输出,这与您在浏览器中访问 original url 时可能得到的结果相匹配
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"
}
}
我在 PHP(laravel) 应用程序中使用 Google web search API 在网络中搜索波斯语(波斯语)单词 ...
我为此使用此代码:
<?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);
英语没有任何问题,但波斯语我没有任何结果...
我该如何解决这个问题?
您需要使用 php 的 urlencode 方法对 url 的波斯语位进行编码。
所以基本上改变你的url的定义如下。
$keyword = urlencode('فارسی');
$url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=$keyword&userip=192.168.106.1";
使用它会得到以下输出,这与您在浏览器中访问 original url 时可能得到的结果相匹配
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"
}
}