1

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 Answers1

0

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>
Raphael Rafatpanah
  • 19,082
  • 25
  • 92
  • 158
  • Uh, I did close the php tag, but still it won't show a thing. – Chinmay Nagrale Aug 03 '15 at 12:03
  • @user3902158, okay, let's begin the debugging process. Please add the updated code and post the output. – Raphael Rafatpanah Aug 03 '15 at 12:05
  • Btw, I just noticed that your `url` isn't accessible to the public unless you're logged in. You can verify this by visiting your url in google chrome's incognito mode. You are redirected to a login page. There is no div with your required id on that page and hence, `html` will be empty. – Raphael Rafatpanah Aug 03 '15 at 12:08
  • So, what should I do then? – Chinmay Nagrale Aug 03 '15 at 12:09
  • Okay, that's because I forgot to echo `$element`. Use the updated code. – Raphael Rafatpanah Aug 03 '15 at 12:10
  • Why do you want to output someone else's content on your site? – Raphael Rafatpanah Aug 03 '15 at 12:12
  • I used the updated code, it shows html was empty. What to do next? – Chinmay Nagrale Aug 03 '15 at 12:12
  • Well, I use that website, and the topic's first post is my online game group's application topic, so I'm working on a website which will echo the most updated topic div and then it will be used like in this website: http://sapd.bl.ee/l?site=app So, I'm trying to do something like that. – Chinmay Nagrale Aug 03 '15 at 12:14
  • I'm looking into how to pass crudentials to `file_get_html`. If there is a way, it can work. Let's mark this question as answered and create a new one, 'How to pass crudentials into `file_get_html` using simplehtmldom. http://simplehtmldom.sourceforge.net/ – Raphael Rafatpanah Aug 03 '15 at 12:19
  • What should I type in the question part then? Quote my question or what? – Chinmay Nagrale Aug 03 '15 at 12:20
  • "I'm looking to use `file_get_html` to get a div from another website into mine. This is for a game website to get the latest relevent post. The problem is that their website requires authentication. Is there a way I can programatically authenticate via php and allow the correct page to be returned when using `file_get_html`?" – Raphael Rafatpanah Aug 03 '15 at 12:23
  • It says I can post once in 90mins. Need to wait till that time.. :( I'll work on other part which I've to. – Chinmay Nagrale Aug 03 '15 at 12:23
  • In the mean time, let's see if we can solve it on our own. Let's open up a chat. – Raphael Rafatpanah Aug 03 '15 at 12:24
  • Let's look at this question: http://stackoverflow.com/questions/27341413/programmatically-log-in-to-ssl-site-with-curl-and-php Do you have `curl` installed on your server? If not, do you have permissions to install it? – Raphael Rafatpanah Aug 03 '15 at 12:25
  • It says I need 20 rep to move this discussion to chat. – Chinmay Nagrale Aug 03 '15 at 12:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/85003/discussion-between-user3902158-and-raphael-rafatpanah). – Chinmay Nagrale Aug 03 '15 at 12:28