Skip to content
Advertisement

How to read data from a webpage in two instances (objects)?

I’m working on this project:

<?php
$curl = curl_init("https://www.timeanddate.com/worldclock/");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($curl);

if(!empty($curl)){
    $thispage = new DOMDocument;
    libxml_use_internal_errors(true);
    $thispage->loadHTML($html);
    libxml_clear_errors();

    $xpath = new DOMXPath($thispage);
    $status = $xpath->evaluate('string(//*[@id="p36"])');
    echo $status;


}

Curl connects to website timeanddate.com and reads data from object //*[@id="p36"] (xpath).

How can I read data from two objects instead of just one?

Advertisement

Answer

I already found out that two variables are necessary:

$xpath = new DOMXPath($thispage);
$status = $xpath->evaluate('string(//*[@id="p36"])');   //first object
$xpath2 = new DOMXPath($thispage);
$status2 = $xpath2->evaluate('string(//*[@id="p28"])'); //second object
echo $status;
echo $status2;

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement