Retrieving Contents of a site. [Solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Retrieving Contents of a site. [Solved]

Post by anthony88guy »

I need get contents of a website, and then use it on my site. Its just the ranks of players and their army sizes. I tried using the following but it doesn’t work. I get this error:
Warning: file_get_contents(http%3A%2F%2Fwww.kingsofchaos.com%2Fbattlefield.php%3Fstart%3D0): failed to open stream: No such file or directory in /home/nokiddin/public_html/test.php on line 11
Do I have to use fopen()?

Code: Select all

<?php
$url = urlencode('http://www.kingsofchaos.com/battlefield.php?start=0');
echo htmlspecialchars(file_get_contents($url)); 
?>
Last edited by anthony88guy on Thu May 05, 2005 7:00 pm, edited 2 times in total.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

look like 5 posts down from yours for more info.

geez people read other posts/search, its there for a reason!
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

Code: Select all

<?php
$site = file("http://www.kingsofchaos.com/stats.php?id=654760");
foreach ($site as $key => $value){
    echo $value;
}
?>
Sorry about not searching, I was in a hurry, also havent try seen this topic around. Anway, So I can display the page. But I need to get speific text from it. How should I go about doing so?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

*yawn*..... This has been asked many times before. For a start, use file_get_contents() to avoid the foreach loop.

Secondly. Use a regular expression to extract the part you need.

What part do you need?
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

d11wtq wrote:*yawn*..... This has been asked many times before. For a start, use file_get_contents() to avoid the foreach loop.

Secondly. Use a regular expression to extract the part you need.

What part do you need?
:lol: :lol: :lol:
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

I am having trouble with using file_get_contents()

Code: Select all

$f = implode("",file("http://www.kingsofchaos.com/stats.php?id=654760"));
$site = file_get_contents($f);
echo $site;
Error: Warning: file_get_contents(

I would like to get the armysize of http://www.kingsofchaos.com/stats.php?id=654760
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

anthony88guy wrote:I am having trouble with using file_get_contents()

Code: Select all

$f = implode("",file("http://www.kingsofchaos.com/stats.php?id=654760"));
$site = file_get_contents($f);
echo $site;
Error: Warning: file_get_contents(

I would like to get the armysize of http://www.kingsofchaos.com/stats.php?id=654760
The Manual wrote:file_get_contents -- Reads entire file into a string

Identical to file(), except that file_get_contents() returns the file in a string, starting at the specified offset up to maxlen bytes. On failure, file_get_contents() will return FALSE.
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

Code:

Code: Select all

<?php
$f = file("http://www.kingsofchaos.com/stats.php?id=654760");
$site = file_get_contents($f);
echo $site;
?>
Error: Warning: file_get_contents() expects parameter 1 to be string, array given in /home/nokiddin/public_html/test.php on line 10
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Code: Select all

<?php
$f = "http://www.kingsofchaos.com/stats.php?id=654760";
$site = file_get_contents($f);
echo $site;
?>
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

Thanks hawleyjr I should of known, so what would be the best way to get specific piece of info?
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

d11wtq wrote: Secondly. Use a regular expression to extract the part you need.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

Code: Select all

&lt;td&gt;&lt;b&gt;Army Size:&lt;/b&gt;&lt;/td&gt;
		&lt;td&gt;3,116,134&lt;/td&gt;
Appears to be the part you want (Army size).

Code: Select all

$found = preg_match ('#<td><b>Army size:</b></td>[\n\s]*<td>([\d,]*?)</td>#', $string, $match);
if ($found):
	echo 'Army size: '.$match[0];
else:
	echo 'No matches.';
endif;
d11wtq | corrected syntax error in regex
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Post by Trenchant »

d11wtq wrote:*yawn*..... This has been asked many times before. For a start, use file_get_contents() to avoid the foreach loop.

Secondly. Use a regular expression to extract the part you need.

What part do you need?
Another perfect moment for someone to JUMP up and say, "I'm gonna write a tutorial."

I also have noticed this topic has been repeated lots.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

If you're writing a tutorial I'd strongly advise to write about fsockopen() over file_get_contents() because some websites reject requests if the User-Agent header is not sent (you can either do this with fsockopen() or using the cURL library).

I have recently completed a project for a client which take a hell of a lot of useful data from any given website (in a similar fashion to a search engine) and I was forced to take the fsockopen() route after noticing numerous rejections with file_get_contents().

If you need a basic example of using fsockopen() to send a HTTP 1.0 or 1.1 request there's some examples in the manual.
Post Reply