Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using PHP to grab the source of a remote webpage

Status
Not open for further replies.

monkle

Programmer
Feb 11, 2004
132
US
I am working on a script that requires grabbing the source html of a remote webpage. I have poured a good bit of time into this already, both in searching for information and in experimentation.

I've already written scripts that access remote files successfully, but I'm running into trouble with dynamically generated web pages. For instance, I can use fread to get the information in test.txt, but I don't know how to get the source html when the url is "
I know this must be possible, but I'm not having any luck finding any information on anything even remotely related. Please point me in the right direction.
 
Thank you. I believe I'm on the right track now, and making good progress.
 
Greetings,

I don't mean to highjack this thread, but...

I would like to create a script which spins through the links in my database and checks for any that are broken. I know this can be achieved by using fopen(), but it there a better php function for this?

Thanks,
Itshim
 
ok, now I made it for you..
ps. it's not very fast!! I think there might be better ways of doing this, but it can be used, let's say.. if you allow your users to post theire homepages to your user database?

or, maybe if someone reports an url "not working", you can double-check it, before the admin gets notified??

anyways, here is the code..
Code:
<?php

$webpage['url'] = "[URL unfurl="true"]http://asdasdiajsdasjd.com";[/URL]
$webpage['title'] = "test-url";

echo check_url($webpage);

function check_url($url, $mode="r") {
	if (!@fopen($url['url'], $mode)) {
	    $res = "Sorry, the page {$url['url']} is offline!<br /> Please check back later!";
	  }
	else {
	    $res = "<a href=\"{$url['url']}\" target=\"_blank\">{$url['title']}</a>";
	  }
  return $res;
} // end function check_url
?>

working and tested!
ps. you might want to control the variables, check if the array is set, etc.

good luck!

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top