Skip to content

Instantly share code, notes, and snippets.

@elm-arata
Created January 5, 2018 09:26
Show Gist options
  • Save elm-arata/62e80ab9faf8d6370a3e1f7ddaca2436 to your computer and use it in GitHub Desktop.
Save elm-arata/62e80ab9faf8d6370a3e1f7ddaca2436 to your computer and use it in GitHub Desktop.
Google Custom Search API でキーワード検索結果を取得
<?php
// 設定
$search_query = "検索語";
$api_key = "APIキー";
$cx = "検索エンジンID";
// 検索用URL
$tmp_url = "https://www.googleapis.com/customsearch/v1?";
// 検索パラメタ発行
$params_list = array(
'q' => $search_query,
'key' => $api_key,
'cx' => $cx,
'alt' => 'json',
'start' => '1',
);
// リクエストパラメータ作成
$req_param = http_build_query($params_list);
// リクエスト本体作成
$request = $tmp_url.$req_param;
// jsonデータ取得
$json = file_get_contents($request, true);
$json_d = json_decode($json, true);
// urlを取得
for ($i = 0; $i < 10; $i++) {
$get_url = $json_d['items'][$i]['link'];
echo "$get_url\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment