I am writing a php where one of its function is to scrape data from internet. Thanks to Barmar, now I can display my scrapping result smoothly in my index.php file. Now, I want to update my scraping result to my database. But before that, I need to access to my scrapping result inside the tag of id “result”. Here is my php:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>contacts.php</title>
</head>
<body>
<div id="result">
</div>
<?php
$tz = 'Asia/Hong_Kong';
$timestamp = time();
$dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
$dt->setTimestamp($timestamp); //adjust the object to correct timestamp
$eng_gov_html = 'https://www.info.gov.hk/gia/wr/' . substr($dt->format('Y/m/d'),0, 4) . substr($dt->format('Y/m/d'),5, 2) . '/'. substr($dt->format('Y/m/d'),8, 2) . '.htm';
$rootFolder = dirname(__DIR__);
require_once $rootFolder.'/Bulletin Translator/private/lib/vendor/autoload.php';
$client = new GuzzleHttpClient();
$response = $client->request('GET', 'https://api.github.com/repos/guzzle/guzzle');
// Send an asynchronous request.
$request = new GuzzleHttpPsr7Request('GET', $chin_gov_html);
$promise = $client->sendAsync($request)->then(function ($response)
{
$doc = new DOMDocument();
$body = (string) $response->getBody();
libxml_use_internal_errors(true);
$doc->loadHTML($body);
$xpath = new DOMXPath($doc);
$bulltin_name = $xpath->evaluate('//body/div/div/div/ul/li/a');
echo '<script>document.getElementById("result").innerHTML = "";</script>';
foreach($bulltin_name as $element)
{
//print_r($element->textContent."<br>");
echo '<script>
for (i = 0; i <'. json_encode($element->textContent, JSON_UNESCAPED_UNICODE) .'.length; i++)
{
if ('. json_encode($element->textContent, JSON_UNESCAPED_UNICODE) .'.substring(i, i+1)!=" ")
{
document.getElementById("result").innerHTML+='. json_encode($element->textContent, JSON_UNESCAPED_UNICODE) .'.substring(i, i+1);
}
}
document.getElementById("result").innerHTML+="</br>";
</script>';
}
echo '<script>
var total_bulletin_number = document.getElementById("result").innerHTML.split("天氣稿第").length - 1;
</script>';
$bulltin_url = $xpath->evaluate('//body/div/div/div/ul/li/a/@href');
foreach($bulltin_url as $url)
{
echo '<script> document.getElementById("result").innerHTML+='. json_encode($url->textContent,JSON_UNESCAPED_UNICODE) .'+ "</br>"</script>';
}
echo '<script>
var content_array = document.getElementById("result").innerHTML.split("<br>");
document.getElementById("result").innerHTML = "";
for (c = total_bulletin_number - 1; c >= 0 ; c--)
{
document.getElementById("result").innerHTML += content_array[c].substring(4, 7) + ": " + content_array[c].substring(9) + ", " + "https://www.info.gov.hk" + content_array[total_bulletin_number + c] + "</br>";
}
document.getElementById("result").removeChild(document.getElementById("result").lastElementChild);
content_array = document.getElementById("result").innerHTML.split("<br>");
</script>';
$result = $doc->getElementById('result')->textContent;
print_r(explode("htm", $result));
});
$promise->wait();
?>
<?php
// Some script here to upload the result to database...
// Some script here to upload the result to database...
// Some script here to upload the result to database...
?>
</body>
</html>
Now, I want to perform an operation in php like this as if in javascript:
var content_array = document.getElementById("result").innerHTML.split(".htm");
var content_arrary_length = content_array.length;
I try to add two lines at the end of my php script:
$result = $doc->getElementById('result')->textContent;
print_r(explode("htm", $result));
print_r(count(explode("htm", $result));
However, an error is returned:
Warning: Attempt to read property "textContent" on null in C:xampphtdocsBulletin Translatorindex.php on line 118
What shall I do to asign a varibale to the result div within the same php file?