マイクロデータ値を抽出したい。
Puttyを使用して、コマンドラインから実行されるYiiアプリを使用します。
次のコードは出力を生成しません。
$ this->input->htmlにhtmlドキュメントソース全体を保存します。
どこかで$content = new DOMXPath($dom);
ひびが入っていると思います。理由がわからない
誰か知っているなら、手を貸してください。
$dom = new DOMDocument();
$html = $this->input->html;
$html = <<<HTML
echo $html;
HTML;
@$dom->loadHTML($html);
echo $html;
$content = new DOMXPath($dom);
print_r($content);
// find price
try {
echo '1'.$this->getMicrodataAttribute($content, 'http://data-vocabulary.org/Offer', 'price');
$this->output->productPrice = $this->getMicrodataAttribute($content, 'http://data-vocabulary.org/Offer', 'price');
//echo 'result output product price: '.$this->output->productPrice.PHP_EOL;
} catch (Exception $e) {
}
// find title
try {
$this->output->productTitle = $this->getMicrodataAttribute($content, 'http://data-vocabulary.org/Product', 'name');
if (!$this->output->productTitle)
if (preg_match("#<title>(.+)<\/title>#iU", $this->input->html, $t)) {
$this->output->productTitle = trim($t[1]);
}
} catch (Exception $e) {
}
これは、マイクロデータ値を抽出する必要がある関数です。
public function getMicrodataAttribute($content, $itemtype, $itemprop) {
$tags = $content->query("//*[@itemtype=\"$itemtype\"]//*[@itemprop=\"$itemprop\"]");
//print_r($tags);
if ($tags) {
foreach ($tags as $tag) {
//die('dd');
if (!$tag->getAttribute('content')) {
return $tag->nodeValue;
}
return $tag->getAttribute('content');
}
}
return null;
}