简单HTMLDOM代码问题
Simple HTML DOM code issue
我正在开发一个页面,该页面将 html div 代码添加到我的网站页面并显示在网站上。我的代码是这样的:
require './simpledom.php';
$url = 'https://cit2.net/index.php?topic=75443.0';
$html = file_get_html($url); if (!empty($html)) {
$element = $html->find('div[id=msg_783326]');
if (!empty($element)) {
echo $element;
}
}
它不会显示任何东西,因为 link 包含其中的东西。我希望它在我的网站页面的那个页面中显示特定的 div。我应该在我的代码中 do/edit 什么,以便它在我的页面中显示特定的 div。
编辑:我的 HTML 代码,我想在其中回显 div。
<div id="main_b">
<div class="wrapper">
<div id="main_content">
<?php
require './simpledom.php';
$url = 'https://cit2.net/index.php?topic=75443.0';
$html = file_get_html($url);
if (!empty($html)) {
$element = $html->find('div[id=msg_783326]');
if (!empty($element)) {
echo $element;
}
}
</div>
</div>
</div>
在您编辑之前,我没有意识到您已经在尝试 echo
$element
到正确的位置。但在您的情况下,您似乎只是忘记了结束 ?>
php 标记。
<div id="main_b">
<div class="wrapper">
<div id="main_content">
<?php
require './simpledom.php';
$url = 'https://cit2.net/index.php?topic=75443.0';
$html = file_get_html($url);
if (!empty($html)) {
$element = $html->find('div[id=msg_783326]');
if (!empty($element)) {
echo $element;
}
}
?>
</div>
</div>
</div>
编辑:还是没有输出任何东西?让我们开始调试过程:
<div id="main_b">
<div class="wrapper">
<div id="main_content">
<?php
require './simpledom.php';
$url = 'https://cit2.net/index.php?topic=75443.0';
$html = file_get_html($url);
if (!empty($html)) {
$element = $html->find('div[id=msg_783326]');
if (!empty($element)) {
echo $element;
} else {
$element = 'element was empty';
}
} else {
$element = 'html was empty';
}
echo $element;
?>
</div>
</div>
</div>
更新:尝试以编程方式登录
<div id="main_b">
<div class="wrapper">
<div id="main_content">
<?php
require './simpledom.php';
$login_url = 'https://cit2.net/index.php?action=login';
$topic_url = 'https://cit2.net/index.php?topic=75443.0';
$username = '';
$password = '';
$params = [
'user' => $username,
'passwrd' => $password
];
$params = http_build_query($params);
// init curl
$ch = curl_init();
// set the url to work with
curl_setopt( $ch, CURLOPT_URL, $login_url );
// enable http post
curl_setopt( $ch, CURLOPT_POST, 1 );
// set the post parameters
curl_setopt( $ch, CURLOPT_POSTFIELDS, $params );
// handle cookies for the login
curl_setopt( $ch, CURLOPT_COOKIEJAR, 'cookie.txt' );
//Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
//not to print out the results of its query.
//Instead, it will return the results as a string return value
//from curl_exec() instead of the usual true/false.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
// execute the request
$store = json_decode( curl_exec( $ch ) );
if ( $store->status == 'ok' ) {
$html = file_get_html($topic_url);
if (!empty($html)) {
$element = $html->find('div[id=msg_783326]');
if (!empty($element)) {
echo $element;
} else {
$element = 'element was empty';
}
} else {
$element = 'html was empty';
}
} else {
$elment = 'request was bad';
}
echo $element;
?>
</div>
</div>
</div>
我正在开发一个页面,该页面将 html div 代码添加到我的网站页面并显示在网站上。我的代码是这样的:
require './simpledom.php';
$url = 'https://cit2.net/index.php?topic=75443.0';
$html = file_get_html($url); if (!empty($html)) {
$element = $html->find('div[id=msg_783326]');
if (!empty($element)) {
echo $element;
}
}
它不会显示任何东西,因为 link 包含其中的东西。我希望它在我的网站页面的那个页面中显示特定的 div。我应该在我的代码中 do/edit 什么,以便它在我的页面中显示特定的 div。
编辑:我的 HTML 代码,我想在其中回显 div。
<div id="main_b">
<div class="wrapper">
<div id="main_content">
<?php
require './simpledom.php';
$url = 'https://cit2.net/index.php?topic=75443.0';
$html = file_get_html($url);
if (!empty($html)) {
$element = $html->find('div[id=msg_783326]');
if (!empty($element)) {
echo $element;
}
}
</div>
</div>
</div>
在您编辑之前,我没有意识到您已经在尝试 echo
$element
到正确的位置。但在您的情况下,您似乎只是忘记了结束 ?>
php 标记。
<div id="main_b">
<div class="wrapper">
<div id="main_content">
<?php
require './simpledom.php';
$url = 'https://cit2.net/index.php?topic=75443.0';
$html = file_get_html($url);
if (!empty($html)) {
$element = $html->find('div[id=msg_783326]');
if (!empty($element)) {
echo $element;
}
}
?>
</div>
</div>
</div>
编辑:还是没有输出任何东西?让我们开始调试过程:
<div id="main_b">
<div class="wrapper">
<div id="main_content">
<?php
require './simpledom.php';
$url = 'https://cit2.net/index.php?topic=75443.0';
$html = file_get_html($url);
if (!empty($html)) {
$element = $html->find('div[id=msg_783326]');
if (!empty($element)) {
echo $element;
} else {
$element = 'element was empty';
}
} else {
$element = 'html was empty';
}
echo $element;
?>
</div>
</div>
</div>
更新:尝试以编程方式登录
<div id="main_b">
<div class="wrapper">
<div id="main_content">
<?php
require './simpledom.php';
$login_url = 'https://cit2.net/index.php?action=login';
$topic_url = 'https://cit2.net/index.php?topic=75443.0';
$username = '';
$password = '';
$params = [
'user' => $username,
'passwrd' => $password
];
$params = http_build_query($params);
// init curl
$ch = curl_init();
// set the url to work with
curl_setopt( $ch, CURLOPT_URL, $login_url );
// enable http post
curl_setopt( $ch, CURLOPT_POST, 1 );
// set the post parameters
curl_setopt( $ch, CURLOPT_POSTFIELDS, $params );
// handle cookies for the login
curl_setopt( $ch, CURLOPT_COOKIEJAR, 'cookie.txt' );
//Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
//not to print out the results of its query.
//Instead, it will return the results as a string return value
//from curl_exec() instead of the usual true/false.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
// execute the request
$store = json_decode( curl_exec( $ch ) );
if ( $store->status == 'ok' ) {
$html = file_get_html($topic_url);
if (!empty($html)) {
$element = $html->find('div[id=msg_783326]');
if (!empty($element)) {
echo $element;
} else {
$element = 'element was empty';
}
} else {
$element = 'html was empty';
}
} else {
$elment = 'request was bad';
}
echo $element;
?>
</div>
</div>
</div>