tags:

views:

237

answers:

3

I'm in the process of rewriting a Perl-based web crawler I wrote nearly 8 years ago in PHP. I used the quite handy URI::URL module in perl to do things like:

$sourceUrl = '/blah.html';
$baseHost = 'http://www.example.com';
my $url = URI::URL->new($sourceUrl, $baseHost);
return $url->abs;

returns: 'http://www.example.com/blah.html'

the parse_url function in PHP is quite handy, but is there something more robust? Specifically something that will give the above functionality?

+1  A: 

Maybe Zend_Uri is what you are looking for?

n3rd
definitely promising, but still doesn't appear to be as smart and flexible.
pixel
A: 
print $baseHost . $sourceURL;

Am I missing something? Your way seems needlessly overcomplicated.

ceejayoz
It's really not. URI objects are a blessing (perl pun intended). So nice to have it when you want to modify parts after you've constructed like changing the scheme, parameters or even auth. Simplifying/correcting the url with canonical, etc. Abstraction and encapsulation are always worth the effort. These are the types of things I miss from Perl now that I am in the very unfortunate world of PHP.
Trey
Perhaps I'm not doing enough 'enterprisey' stuff, but in years of PHP I've yet to come across a situation where the extra lines of code you posted was worth the effort. I'd just create a baseurl() function that gives the scheme, parameters, auth, etc. and concatenate (or use a framework which already does that for me).
ceejayoz
A: 

I did a bit of searching on the PEAR archive, and my first-guess approximation of URI::URL is Net_URL2. Maybe you want to give that a shot?

Tony Miller
Net_URL2 is nice, although it doesn't seem to handle things properly. i.e., starting with a not fully qualified URL of 'page.html', I check the $url->hostname to see if it's empty (which it is), then do $url->setHost('www.example.com') and for some reason $url->getUrl() returns 'www.example.compage.html'... it's just not smart enough. I'm about to the point of taking URI::URL and converting it to PHP. :)
pixel
If you do, that would certainly be a nice contribution to the PHP community!
Tony Miller