Jump to content

Grabbing Values from other websites, is it possible?


bicho83

Recommended Posts

Hello, I am new to php.

 

I was wondering if is possible to grab values from other websites.

 

What I want to do is from a website that shows a bunch of data, and grab particular values from it, and show it in my website.

 

I hope I am making sense. Maybe with XML? Are there resources on the web that would me help me, or examples that I can see to make this possible?

 

Thanks in advance.

Link to comment
Share on other sites

Depending on how advanced the external website is, or what type of credentials it requires, you will either be using simple functions such as:

- file(), file_get_contents() - you will use these for grabbing text from a single webpage where no special data is required

- fsockopen()/fgets()/fputs() - you will uses these if you need to login/post data in order to get to the page you are referring to

 

From there, you will store the output of the webpage in a variable and you can use regex to match whatever you need.

Link to comment
Share on other sites

Thanks for replies.

 

I don't just want to grab the website, but certain values of it.

 

For example, if the website: http://www.example.com/stats.html has the following html:

 

<table width="100%" border="0" cellpadding="0" cellspacing="0" class="t_top">
					<tr>
						<td>Global Points</td>

						<td class="ltd">134299</td>
					</tr>
					<tr>
						<td>Career Points</td>
						<td class="ltd">255838</td>
					</tr>
</table>

I want the values of the html table "Global Points" which are 134299. I want to publish just that value in my website. How would I do that?

 

I try playing with file_get_content, and file_put_content. But I don't know if php has the possibility of grabbing specific values in files, or html tables. I also played with preg_match. But preg_match only searches for the ocurrences of the matched value. It tells you if it found it or not. I don't want to count it, but grabbed it. This is probably when Javascript steps in, i rather do it in one language.

 

Link to comment
Share on other sites

file_get_contents() will get the entire contents of the page. If you could guarantee that the page had perfect XHTML, you could use an XML Parser module (like SimpleXML) to get the values easily, but 99.9% of the time you can't rely on that. So, we need to look at the page like a giant string.

 

preg_match() does only return IF it found it or not, but you can pass a 3rd argument by reference and it will populate it with the matches. Check out http://us.php.net/preg_match to find out more about how to use the matches. Here is an example on how to get the value:

 

<?php
  $url = "http://www.example.com/stats.html"; //Page to search
  $matches = array(); //Variable to be populated with the match values

  $contents = file_get_contents($contents) //Get the page contents
    or die("Could not get page contents");
  if(!preg_match("/<td>Global Points<\/td>\s*<td class=\"ltd\">(\d+)<\/td>/",$contents,$matches)) //Find value on page
    die("Could not find value");
  $g_points = $matches[1]; //Store match

  echo "Global Points: {$g_points}"; //Print value
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.