誰かに少し時間をかけて私のコードを見てもらいたいです。いくつかのニュース コンテンツを解析しています。ニュースの URL とタイトルを含む最初の解析をデータベースに挿入できます。さらに拡張して、各記事のリンクを渡し、記事の内容を解析してデータベースに含めたいと考えています。最初の解析は次のように完全に機能します。
<?php
include_once ('connect_to_mysql.php');
include_once ('simple_html_dom.php');
$html = file_get_html('http://basket-planet.com/ru/');
$main = $html->find('div[class=mainBlock]', 0);
$items = array();
foreach ($main->find('a') as $m){
$items[] = '("'.mysql_real_escape_string($m->plaintext).'",
"'.mysql_real_escape_string($m->href).'")';
}
$reverse = array_reverse($items);
mysql_query ("INSERT IGNORE INTO basket_news (article, link) VALUES
".(implode(',', $reverse))."");
?>
ご覧のとおり、私はPHP Simple HTML DOM Parser を使用しています。 拡張するには、すべての html タグがデータベースに挿入されるようにパラメーターをバインドできる mysqli ステートメントを使用しようとしています。以前に XML 解析でこれを行ったことがあります。問題は、配列をバインドする方法がわからないことです。コードが正しいかどうか、このように機能するかどうかを確認してください...コード全体は次のとおりです。
<?php
$mysqli = new mysqli("localhost", "root", "", "test");
$mysqli->query("SET NAMES 'utf8'");
include_once ('simple_html_dom.php');
$html = file_get_html('http://basket-planet.com/ru/');
//find main news
$main = $html->find('div[class=mainBlock]', 0);
$items = array();
foreach ($main->find('a') as $m){
$h = file_get_html('http://www.basket-planet.com'.$m->href.'');
$article = $h->find('div[class=newsItem]');
//convert to string to be able to modify content
$a = str_get_html(implode("\n", (array)$article));
if(isset($a->find('img'))){
foreach ($a->find('img') as $img){
$img->outertext = '';}} //get rid of images
if(isset($a->find('a'))){
foreach ($a->find('a') as $link){
$link->href = 'javascript:;';
$link->target = '';}} //get rid of any javascript
if(isset($a->find('iframe'))){
foreach ($a->find ('iframe') as $frame){
$frame->outertext = '';}} //get rid of iframes
@$a->find('object', 0)->outertext = '';
@$a->find('object', 1)->outertext = '';
//modify some more to retrieve only text content
//put entire content into a div (will if statements work here???)
$text_content = '<div>'.$a.'<br>'.
($a->find('object', 0)->data > 0 ? '<a target="_blank" href="'.$a->find('object', 0)->data.'">Play Video</a> ')
($a->find('object', 1)->data > 0 ? '<a target="_blank" href="'.$a->find('object', 1)->data.'">Play Video</a> ')
($a->find('iframe[src*=youtube]', 0)->src > 0 ? '<a target="_blank" href="'.$a->find('iframe', 0)->src.'">Play Video</a> ')
//couple more checks to see if video links are present
.'</div>';
$items[] = '("'.$m->plaintext.'","'.$m->href.'","'.$text_content.'")';
}
//reverse the array so the latest items have the last id
$reverse = array_reverse($items);
$stmt = $mysqli->prepare ("INSERT IGNORE INTO test_news (article, link, text_cont) VALUES (?,?,?)");
$stmt->bind_param ???; //(implode(',', $reverse));
$stmt->execute();
$stmt->close();
?>
したがって、ロジックは、見つかった記事のすべての href に対するものであり、それを渡してコンテンツを解析し、それを配列に追加しようとしています。おそらく大量のエラーがありますが、バインドして機能するかどうかを確認する方法がわからないため、まだテストできません。また、 $text_content div 内で if ステートメントを実行できるかどうかもわかりません...存在する場合は「ビデオの再生」を表示することを意味します。誰かが私と一緒にこれに取り組むために時間を割いてくれるなら、私は本当に感謝しています。
更新: $text_content div の if ステートメントを比較演算子に変更しました。