简单的HTML DOM代码问题

问题描述:

我正在开发一个页面,该页面将html div代码添加到我的网站页面并显示在网站上。我的代码是这样的:简单的HTML DOM代码问题

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显示在我的页面中。

编辑:我的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> 
+0

您将需要返回$元素变量,并将其放置在您的视图的特定专区内。 –

+0

那么,我不能把它放在我想要的任何地方? –

+0

你当然可以。请同时发布html代码,包括您想要放置$元素的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> 

UPDATE:尝试登录编程

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

呃,我确实关闭了php标签,但它仍然不会显示任何东西。 –

+0

@ user3902158,好吧,让我们开始调试过程。请添加更新的代码并发布输出。 –

+0

顺便说一句,我刚刚注意到,除非您已经登录,否则您的“url”不会被公众访问。您可以通过在Chrome浏览器的无痕模式下访问您的网址来验证此问题。您被重定向到登录页面。在该页面上没有您需要的ID的div,因此'html'将是空的。 –