Simple HTML DOM code issue

三世轮回 提交于 2019-12-08 05:13:01

问题


I'm working on a page that gets html div code to my website page and displays on the website. My code goes like this:

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;
        }
     }

It won't show a thing where as the link contains things in it. I want it to show the specific div in that page in my website page. What should I do/edit in my code so it shows the specific div into my page.

EDIT: My HTML code under which I want to echo the 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>

回答1:


Before your edit I didn't realize that you were trying to echo $element into the correct place already. But it seems like in your case you simply forgot the closing ?> php tag.

<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>

EDIT: Still not outputting anything? Let's start the debugging process:

<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>

UPDATE: Try to login programatically

<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>


来源:https://stackoverflow.com/questions/31786067/simple-html-dom-code-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!