Re: HTML_DocumentDependencies / Net_URL problem

From: Date: Wed, 25 Jan 2006 15:02:50 +0000
Subject: Re: HTML_DocumentDependencies / Net_URL problem
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-41084@lists.php.net to get a copy of this message
Christoph Lütjen - webEdition Software GmbH a écrit :
Hi! Hello Christoph,
Short time ago I started writing the HTML_DocumentDependencies class which is intended to have an easy way to parse an HTML file and to get a list of links, images,.. which could be stored in a database or wherever. (http://pear.php.net/pepr/pepr-proposal-show.php?id=338) (and to learn how to add classes to PEAR ;-) I'm late to post a comment on this proposal (Status: Called for Votes), but i think, even if this package is a good idea, its too much restrictive.
Try to parse a document that is a dynamic html page (php script) ... <a href="<?php echo $faq; ?>">FAQ</a> ... and you may have results such as : destination = <?php echo $faq; ?>
type	        = link
Another example : Try to parse an Office HTML Word document (its impossible); btw it could be very could. This package does not check validity of such sources(IN), and produced un-expected results. Do you have think of that ?!
Now Justin told me to use the Net_URL class to represent urls found in document. Makes sense, but now I’ve a problem with the Net_URL class… It seems that it’s not possible to set the “context” of the url, meaning that I cannot define to which url a relative url is relative. (“./file.html” makes no sense if I don’t know where “.” is. ) Justin has right to suggest you to use Net_URL
I described this behaviour in a feature request: http://pear.php.net/bugs/bug.php?id=6541 which was closed as “Bogus”. So now I’m a little confused because currently I see no other way than parsing the (part of an) url and create a full uri if I want to have the right data in Net_URL object. … Would be great if someone could take a look at it and give me a hint if ‘v overlooked something… For your problem, i think it's easy (or i don't really understand your need).
Related to Bug#6541 description: Example: http://host1/parser.php opens http://host2/text.html and reads a list of links in text.html. Returns list as array of Net_URL objects. If text.html contains an url like "/link.html" the url object will look like this: Net_URL object [host] => 'host1' ... Should be [host] => 'host2' If you use HTTP_Request, solution is <?php require_once 'Net/URL.php'; require_once 'HTTP/Request.php'; $uri = 'http://host2/text.html'; $req = &new HTTP_Request($uri); $req->setMethod(HTTP_REQUEST_METHOD_GET); $req->sendRequest(); $response1 = $req->getResponseBody(); $dom = new DOMDocument(); @$dom->loadHTML($response1); $tags = $dom->getElementsByTagName('a'); foreach ($tags as $tag) {
    $dest = $tag->getAttribute('href');
    $url =& new Net_URL($dest);
    print_r($url);
} ?> and print_r($url) will give something with host = host1 while you open doc(text.html) on host2
Thanks. Chris Hope it will help
Laurent

« previous php.pear.dev (#41084) next »