googleに検索したいキーワードを入力して、1番目の検索結果を
取得したいと考えています。
http://piyopiyocs.blog115.fc2.com/blog-entry-1063.html
上記のサイトを参考に(ほとんどそのままで)コードを打ちました。
PHP
1<?php 2 3//------------------------------------ 4// 定数設定 5//------------------------------------ 6//APIキー 7$apiKey = "【取得したAPIキー】"; 8 9//検索エンジンID 10$searchEngineId = "【取得した検索エンジンID】"; 11 12// 検索用URL 13$baseUrl = "https://www.googleapis.com/customsearch/v1?"; 14 15//取得開始位置 16$startNum = 1; 17 18//-------------------------- 19// 検索キーワード取得 20//-------------------------- 21$query = "PHP site:piyopiyocs.blog115.fc2.com"; 22 23//------------------------------------ 24// リクエストパラメータ生成 25//------------------------------------ 26$paramAry = array( 27 'q' => $query, 28 'key' => $apiKey, 29 'cx' => $searchEngineId, 30 'alt' => 'json', 31 'start' => $startNum 32 ); 33$param = http_build_query($paramAry); 34 35//------------------------------------ 36// 実行&結果取得 37//------------------------------------ 38$reqUrl = $baseUrl . $param; 39$retJson = file_get_contents($reqUrl, true); 40$ret = json_decode($retJson, true); 41 42//------------------------------------ 43// 結果表示 44//------------------------------------ 45 46//画面表示 47var_dump($ret);
ここで一番下にある$retのdumpは
json
1array(4) { ["kind"]=> string(19) "customsearch#search" ["url"]=> array(2) { ["type"]=> string(16) "application/json" ["template"]=> string(683) "https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json" } ["queries"]=> array(1) { ["request"]=> array(1) { [0]=> array(9) { ["title"]=> string(58) "Google Custom Search - PHP site:piyopiyocs.blog115.fc2.com" ["totalResults"]=> string(1) "0" ["searchTerms"]=> string(35) "PHP site:piyopiyocs.blog115.fc2.com" ["count"]=> int(10) ["startIndex"]=> int(1) ["inputEncoding"]=> string(4) "utf8" ["outputEncoding"]=> string(4) "utf8" ["safe"]=> string(3) "off" ["cx"]=> string(33) "014377105788684933467:ugpudjuaxnw" } } } ["searchInformation"]=> array(4) { ["searchTime"]=> float(0.775363) ["formattedSearchTime"]=> string(4) "0.78" ["totalResults"]=> string(1) "0" ["formattedTotalResults"]=> string(1) "0" } }
と表示されAPIは成功しているようなのですが、JSONの一番右端にある
["formattedTotalResults"]とその左にある["totalResults"]が0件となって
います。
どうすればgoogle検索結果を取得できるのでしょうか?
ご教授お願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。