0

I am new to php curl, can you help me to write code for grabbing gold value from this page http://www.domain.com/gold.php

from following line i just want 27742.00

Gold price today in India (Rs/10gm) is 27742.00 (-67)

Does it require HTML parser or just curl can do it alone?

Thanks for your help.

seoppc
  • 2,766
  • 7
  • 44
  • 76

3 Answers3

2

Regex can do this for you.

preg_match('#Gold price today in India <b>\(Rs\/10gm\)</b> is <b>([0-9\.]+)#', file_get_contents('http://www.marketonmobile.com/gold_price_india.php'), $matches);
echo 'The price is: '.$matches[1];
Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
1

You can retrieve the page with curl or file_get_contents(). When you have the page, you can get the value with a HTML parser or a regex.

Community
  • 1
  • 1
Sjoerd
  • 74,049
  • 16
  • 131
  • 175
0

Does it require HTML parser or just curl can do it alone?

cUrl alone can not parse HTML, so you need a HTML/Text parser that works on the return value of the cUrl request.

Example HTML parser with xpath:

$url = 'http://www.domain.com/gold.php';
$expr = 'string(//div[@class="gold"]/b[2]/text())';

$doc = new DOMDocument();
@$doc->loadHTMLFile($url);
$xp = new DOMXPath($doc);
$price = trim($xp->evaluate($expr));
seoppc
  • 2,766
  • 7
  • 44
  • 76
hakre
  • 193,403
  • 52
  • 435
  • 836