LoginSignup
6
5

More than 5 years have passed since last update.

Yahoo!検索API経由でページの本文を取得するサンプル

Last updated at Posted at 2012-03-16

Zend_Rest_Clientを使っています。

<?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());
}

h3. 参考

http://developer.yahoo.co.jp/webapi/search/premium.html
http://framework.zend.com/manual/ja/zend.rest.client.html

6
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5