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.
seekinG1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
seekinG1Author Commented:
EASY TO FOLLOW (very important for beginners.)
Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.