Page 1 of 2
Retrieving Contents of a site. [Solved]
Posted: Mon May 02, 2005 4:02 pm
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));
?>
Posted: Mon May 02, 2005 4:11 pm
by shiznatix
look like 5 posts down from yours for more info.
geez people read other posts/search, its there for a reason!
Posted: Mon May 02, 2005 4:13 pm
by hawleyjr
Posted: Mon May 02, 2005 5:54 pm
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?
Posted: Mon May 02, 2005 6:04 pm
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?
Posted: Mon May 02, 2005 6:44 pm
by hawleyjr
Posted: Mon May 02, 2005 7:35 pm
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
Posted: Mon May 02, 2005 7:37 pm
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.
Posted: Mon May 02, 2005 7:42 pm
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
Posted: Mon May 02, 2005 7:43 pm
by hawleyjr
Code: Select all
<?php
$f = "http://www.kingsofchaos.com/stats.php?id=654760";
$site = file_get_contents($f);
echo $site;
?>
Posted: Mon May 02, 2005 11:00 pm
by anthony88guy
Thanks hawleyjr I should of known, so what would be the best way to get specific piece of info?
Posted: Mon May 02, 2005 11:08 pm
by hawleyjr
d11wtq wrote:
Secondly. Use a regular expression to extract the part you need.
Posted: Tue May 03, 2005 4:34 am
by vigge89
Code: Select all
<td><b>Army Size:</b></td>
<td>3,116,134</td>
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
Posted: Tue May 03, 2005 8:28 am
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.
Posted: Tue May 03, 2005 8:37 am
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.