Skip to content

Instantly share code, notes, and snippets.

@hirak
Created December 10, 2011 02:28
Show Gist options
  • Save hirak/1454330 to your computer and use it in GitHub Desktop.
Save hirak/1454330 to your computer and use it in GitHub Desktop.
Yahoo!検索API経由でページの本文を取得するサンプル ref: http://qiita.com/items/1337
<?php
require_once 'Zend/Rest/Client.php';
//アプリケーションIDは各自で取得したものを使ってください。
$appid = 'プレミアム検索対応のアプリケーションIDをここに記入';
//APIのベースURLを指定する
$client = new Zend_Rest_Client('http://search.yahooapis.jp/PremiumWebSearchService/V1/webSearch');
//パラメータを設定
$client
->appid($appid)
->query('知恵袋')
;
//リクエスト!
$response = $client->get();
//レスポンスからURLだけを収集
foreach ($response->Result as $res) {
$urls[(string)$res->Title] = (string)$res->Url;
}
//(デバッグ出力)
var_dump($urls);
//ひとつひとつ実際の本文を取得
foreach ($urls as $title => $url) {
echo PHP_EOL,"$title:",PHP_EOL;
//本文はHTMLなのでXML専用のZend_Rest_Clientは使わず、
//Zend_Http_Clientを使用する。
$client = new Zend_Http_Client($url);
$responseObj = $client->request();
//本文抽出
var_dump($responseObj->getBody());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment