简体   繁体   中英

How to use PHP Simple HTML DOM to get data from a remote page traced by a $_GET item?

When a link in A.html is clicked :

<tr>
<td class="book"><a class="booklink" ref="../collection?file=book1.pdf">
Good Read
</a>
-Blah blah blah
</td>  
</tr>

?file=book1.pdf is passed to B.html:

<?php
$src = $_GET['file'];
?>

<iframe src="<?php echo $src; ?>" >
</iframe>

QUESTION:- How to retrieve the text "Good Read-Blah blah blah" from A.html and paste it into the meta description in B.html by using simple html dom? (Please know that there are thousand of listed data in the table in A.html)

Thank you.

Use DOM to load your HTML document and XPath to search it .

// note: if the HTML to parse has bad syntax, use: libxml_use_internal_errors(true);

$doc = new DOMDocument;
$doc->loadHTML(file_get_contents('A.html'));
if ($doc === false) {
    throw new RuntimeException('Could not load HTML');
}

$xpath = new DOMXPath($doc);
$xpathResult = $xpath->query("//a[@href = '../collection?file={$_GET['file']}']/..");
if ($xpathResult === false) {
    throw new LogicException('Something went wrong querying the document!');
}

foreach ($xpathResult as $domNode) {
    echo 'Link text: ' . htmlentities($domNode->textContent) . PHP_EOL;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM