我正在开发一个页面,它将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;
}
}
它不会显示链接中包含内容的东西。我希望它在我的网页中显示该页面中的特定div。我应该在我的代码中做什么/编辑,以便在我的页面中显示特定的div。
编辑:我想要回复div的HTML代码。
<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>
答案 0 :(得分:0)
在您修改之前,我没有意识到您已经尝试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>