• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 465
  • Last Modified:

How to extract data from a webpage and incorporate into my site?

I need to extract a certain text from a webpage and incorporate it into my site. Display it live, add it to my database.
How can I do that? I'm a beginner php - Mysql.
0
seekinG1
Asked:
seekinG1
1 Solution
 
hieloCommented:
//test.php for YOUR site
<?php
$data=file_get_contents('http://www.othersite.com');

echo $data;
?>
0
 
leakim971PluritechnicianCommented:
Hello seekingG1,

Only a text ? Or HTML,XHTML or XML ?

Regards.
0
 
ZeraclesCommented:
The simpliest way will be using curl (included in PHP).

$ch = curl_init(); //init curl

$url="http://example.com"; //here is the page address you want
       curl_setopt($ch, CURLOPT_URL, $url); //here the url is passed to curl


        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //return the transfer as a string
   
        $output = curl_exec($ch); // $output contains the output string
        curl_close($ch);
echo $output;
Read more at http://php.net/manual/en/book.curl.php



0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
seekinG1Author Commented:
hielo: Thanks! I could get the whole page. now how can I extract certain areas? plz see example below.

leakim971: For instance, I like to be able to display the latest stock price. In AAPL example it is contained in id of yfs_l10_aapl in the page. http://finance.yahoo.com/q?s=Aapl

Zeracles: unfortunately I got this error: Fatal error: Call to undefined function curl_init() in C:\wamp\www\test.php  on line 11


0
 
goinbonkersCommented:
you want to look at preg_match_all.  


to match a regular expression to a string and store it to an array:

<?php

$regex = "/abc/";
$string = "abcdefghi";

preg_match_all($regex, $string, $array);

print_r($array[0]);
?>

returns:

Array ( [0] => abc )

0
 
hieloCommented:
On that same page you posted:
http://finance.yahoo.com/q?s=Aapl

If you look closer, below the chart there is a box. Within that box there is a link labeled "Download Data". Use that links instead of http://finance.yahoo.com/q?s=Aapl!

Regards,
Hielo
0
 
seekinG1Author Commented:
hielo: true. I can download the historical data manually. but is there a way to extract the price and display it live?
0
 
hieloCommented:
download simple_html_dom.php:
http://simplehtmldom.sourceforge.net/


<?php
include("simple_html_dom.php");
$html=file_get_html("http://finance.yahoo.com/q?s=AAPL");
$rows=$html->find('div[id=yfi_quote_summary_data] table tr');
$result=array();
foreach($rows as $row){
	preg_match('#([^:]+):(.+)#',strip_tags($row),$m);
	$result[]=array("label"=>$m[1],"value"=>$m[2]);
}
print_r($result);
?>

Open in new window

0
 
seekinG1Author Commented:
EASY TO FOLLOW (very important for beginners.)
Thank you!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now