1

我正在处理一个页面,该页面将 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>
4

1 回答 1

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>
于 2015-08-03T11:59:35.237 回答