0

Okay, so I have the following function that grabs the web page I need:

function login2($url2) {
   $fp = fopen("cookie.txt", "w");
   fclose($fp);
   $login2 = curl_init();
   curl_setopt($login2, CURLOPT_COOKIEJAR, "cookies.txt");
   curl_setopt($login2, CURLOPT_COOKIEFILE, "cookies.txt");
   curl_setopt($login2, CURLOPT_TIMEOUT, 40000);
   curl_setopt($login2, CURLOPT_RETURNTRANSFER, TRUE);
   curl_setopt($login2, CURLOPT_URL, $url2);
   curl_setopt($login2, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
   curl_setopt($login2, CURLOPT_FOLLOWLOCATION, false);

   [...]

I then issue this to use the function:

echo login2("https://example.com/clue/holes.aspx");

This echoes the page I am requesting but I only want it to echo a specific piece of data from the HTML source. Here's the specific markup:

<h4>
   <label id="cooling percent" for="symbol">*</label>
   4.50
</h4>

The only piece of information I want is the figure, which in this specific example is 4.50.

So how can I go about this and make my cURL grab this and echo it instead of echoing the entire page?

silkfire
  • 24,585
  • 15
  • 82
  • 105

1 Answers1

0

You can solve this with XPath:

$html = login2('https://example.com/clue/holes.aspx');

$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

$value = $xpath->query('//label[@id="ctl00_ctl00_PageContainer_MyAccountContainer_symPound"]/following-sibling::text()')->item(0)->nodeValue;

echo $value;
silkfire
  • 24,585
  • 15
  • 82
  • 105