bicho83 Posted February 13, 2008 Share Posted February 13, 2008 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 https://forums.phpfreaks.com/topic/90916-grabbing-values-from-other-websites-is-it-possible/ Share on other sites More sharing options...
Bauer418 Posted February 13, 2008 Share Posted February 13, 2008 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 https://forums.phpfreaks.com/topic/90916-grabbing-values-from-other-websites-is-it-possible/#findComment-465947 Share on other sites More sharing options...
rhodesa Posted February 13, 2008 Share Posted February 13, 2008 Here is a similar topic from yesterday: http://www.phpfreaks.com/forums/index.php/topic,181918.msg812829.html Link to comment https://forums.phpfreaks.com/topic/90916-grabbing-values-from-other-websites-is-it-possible/#findComment-465949 Share on other sites More sharing options...
Rohan Shenoy Posted February 13, 2008 Share Posted February 13, 2008 use a combination of PHP file() function and javascript innerHTML capabilities. This is just a rough thought. It may need furnishing Link to comment https://forums.phpfreaks.com/topic/90916-grabbing-values-from-other-websites-is-it-possible/#findComment-465951 Share on other sites More sharing options...
rhodesa Posted February 13, 2008 Share Posted February 13, 2008 You won't be able to use JavaScript to get content from websites on another domain, it violates the browsers security specs. But, if you wrote a PHP script to play middle man, you should be able to get it working. Link to comment https://forums.phpfreaks.com/topic/90916-grabbing-values-from-other-websites-is-it-possible/#findComment-465957 Share on other sites More sharing options...
bicho83 Posted February 14, 2008 Author Share Posted February 14, 2008 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 https://forums.phpfreaks.com/topic/90916-grabbing-values-from-other-websites-is-it-possible/#findComment-466522 Share on other sites More sharing options...
kenrbnsn Posted February 14, 2008 Share Posted February 14, 2008 You need to use regular expressions to do the search. I can't help you there, since I'm very weak with them. Ken Link to comment https://forums.phpfreaks.com/topic/90916-grabbing-values-from-other-websites-is-it-possible/#findComment-466525 Share on other sites More sharing options...
rhodesa Posted February 14, 2008 Share Posted February 14, 2008 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 https://forums.phpfreaks.com/topic/90916-grabbing-values-from-other-websites-is-it-possible/#findComment-466712 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.