TBa
TBa

Reputation: 33

DOMXPath help needed

I make a code that search for a specific document name (example: SZ-1000) and collect all the links what div class="box" contain.(index.php) The document name maybe contain one or two documents with an ID. (26904, 26905) It works like a charm. I get the IDs back.

But I want to list all the attachments that links contains as links. The trick is now, there's no div element, only table or dd to specific the attachments locations.

I think something wrong with that section:

$xpath->query('//table[@id="content clear-block"]');

Catchable fatal error: Object of class DOMNodeList could not be converted to string in C:\AppServ\www\test\import.php on line 35

Result of var_dump($articles) in import.php;

object(DOMNodeList)#3 (0) { }
object(DOMNodeList)#3 (0) { }

My code:

index.php

$site = 'http://192.168.0.1:81/?q=search/node/SZ-1000';
$html = file_get_contents($site);
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$articles = $xpath->query('//div[@class="box"]');

$links = array();
   foreach($articles as $container) {
   $arr = $container->getElementsByTagName("a");
     foreach($arr as $item) {
      $href =  $item->getAttribute("href");
      $links[] = $href;
     }
}
   foreach($links as $link){
     $text = end(split('/',$value));
     echo $text."<br>";
     $wr_out = file_get_contents("http://127.0.0.1/test/import.php?value=".$text);
     echo $wr_out;
  }

import.php

$value = $_GET['value'];
$site = 'http://192.168.0.1:81/?q=node/'.$value;
$html = file_get_contents($site);
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$articles = $xpath->query('//table[@id="content clear-block"]');

$links = array();
   foreach($articles as $container) {
   $arr = $container->getElementsByTagName("a");
      foreach($arr as $item) {
      $href =  $item->getAttribute("href");
      $links[] = $href;
      echo $href;
     }
}

Thanks for your reply!

Edit:

The 'Catchable fatal error: Object of class DOMNodeList could not be converted to string in C:\AppServ\www\test\import.php on line 35'

error fixed:

echo $wr_out->tagName;

Upvotes: 0

Views: 116

Answers (1)

TBa
TBa

Reputation: 33

Oookay, finally i made it. Here's the solution for the after-ages. With UTF-8 char encoding.

index.php

    <?php

// get some variables from an external source | i use it with Google spreadsheet

    $get = $_GET['get'];
    $site = 'http://192.168.0.1:81/?q=search/node/'.$get;
    $html = file_get_contents($site);

    //libxml_use_internal_errors(true);

    $doc = new DOMDocument();
    $doc->loadHTML($html);

    $xpath = new DOMXpath($doc);
    $articles = $xpath->query('//div[@class="box"]');

    if(!empty($articles)){

    $links = array();
    foreach($articles as $container) {
       $arr = $container->getElementsByTagName("a");
       foreach($arr as $item) {
          $href =  $item->getAttribute("href");
          $links[] = $href;
       }
    }
    $wr_out = "";

    foreach($links as $value){
        $text = end(split('/',$value));
        $wr_out.=file_get_contents("http://127.0.0.1/projekt/search/import.php?value=".$text);

    }

    if(empty($wr_out))
        echo "There is no document with that ID";
        else
    echo $wr_out;
    }
    else
    echo "There is no document with that ID";
    ?>

import.php

    $value = $_GET['value'];
    $site = 'http://192.168.0.1:81/?q=node/'.$value;
    $html = file_get_contents($site);


    //libxml_use_internal_errors(true);

    $doc = new DOMDocument();
    $doc->loadHTML($html);



    $elements = $doc->getElementsByTagName('tbody');
    $table = $elements->item(0);

    $rows = $table->childNodes;

        foreach ($rows as $node) {

          if($node->tagName == "tr"){

            $a = $node->firstChild->firstChild;

             foreach ($a->attributes as $attr) {
                if($attr->nodeName == "href"){
                    $value = $attr->nodeValue;
                    ?>
                        <!doctype html>
                        <head>
                            <title>Search</title>
                          <meta charset="UTF-8">
                        </head>
                        <body>
                            <table align="center">

                                <tr>
                                    <td></td>
                                    <td class="style-1">
                                    <br><h3>
                                    <?=$value?> | <a href='<?=$value?>'>LINK</a></h3><hr>
                                    </td>
                                </tr>
                            </table>
                        </body><?

                }
            }
         }
    }?>

Upvotes: 1

Related Questions