Accessing other websites Accessing other sites with PHP is extremely easy. But why would you want to access another website? If you just want to get information from a website, for instance a website that has a weather report on a city that is of interest to you, then you can write a spider and get that information and more. I said accessing websites with PHP is easy because you access a website in pretty much the same way that you would access a text file on your hard drive, i.e. by using fopen(). fopen(http://localhost/websecure/ftp.php, “r”); The fopen() function that we use to open files is also used to open web pages, because a web page is essentially a file on a server. fopen() has the following modes:
You will only be able to open a file for reading, unless the permissions are set otherwise. Also you will have to use a trailing slash after a directory because fopen does not support redirects. For example: fopen(“http://localhost/websecure/ftp.php/”, “r”); the above example is fine, but the one below will fail: fopen(http://localhost/websecure/ftp.php, “r”); Once you’ve opened a file, you can pretty much treat it the same way you would any other file, using common functions such as file() or fgets() to place or retrieve the data. An example in code might be something like this: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <form action="tester.php" method="get"> <input name="url" type="text" /> <input name="" type="button" /> </form> </body> </html> And the processing code might look something like this: <? $url=$_POST['url']; $fp=file($url); $n=count($fp); $r=rand(0,($n-1)); echo trim($fp[$r]); echo $n; ?>
blog comments powered by Disqus |
|
|
|
|
|
|
|