Your script is missing error handling and checking for data integrity in general.
Then when there is a slightest problem somewhere it might crash at a pretty unrelated different place (spooky action in the (nearer) distance, this at least looks like a straight forward script).
Some little tricks:
$tmp = explode('-', $current);
The beloved powerful explode(), it has a limit (third parameter) and you can provide defaults:
#- three default values, we take null
#
$tmp = explode('-', $current, 3) + [null, null, null];
#
#-- three array entries max
Now then those values are used, you should tell what they are, specifically when you put them into an URL (you don't want it to break at all cost), URLs need to be correct, always.
Better use sprintf(), here I assume you're using positive numbers:
$url = sprintf(
'%s/next/soccer/?year=%d&month=%d&day=%d',
$siteURL, $tmp[0], $tmp[1], $tmp[2]
);
Better options there are with http_build_query() for building the query info part of an URI (I last used it in this answer), point in case is just that you need to encode it properly. Better productions likely then look like:
$date = array_combine(['year', 'month', 'day'], explode('-', $current));
This already shows the name + error handling (warning PHP < 8, ValueError since PHP 8). So more speaking and safe with current PHP, no need for the limit parameter of explode() here any longer!
$date = array_map(intval(...), $date);
Now every date item is integer. You may or may not need it when using
$date = array_combine(['year', 'month', 'day'], explode('-', $current));
$url = "{$siteUrl}/next/soccer/?" . http_build_query($date);
Sweet? But if you don't use http_build_query(), you need the intval() calls otherwise the URL risks to break.
Now so much for URL preparation. Let's check the place of use:
$dom = new DOMDocument();
$dom->loadHTML(execCURL($url))
|| throw new Error('HTML load error from: execCURL($url)');
Check function/mehtod returns, here loadHtml() returns false if there was an error. You want to handle it before proceeding. At least you want to know about it early, hence there (PHP 8.0+) directly throwing with the or "||" condition. This pattern you can use to integrate it quickly. Better throw an error then endless hours of debugging later errors.
Doing so will also show you that the error message then is not useful, which then will make you improve the code automatically at the right places.
Here it's that likely execCURL() does not return valid HTML (oh now hakre, that's clever!), perhaps not even a string(!). So we could have two classes of error, those curl already is able to detect, communicate (and perhaps even handle), and then the problem that the HTML Text (the document) itself is broken.
Not in your case. In your case the error is elsewhere, however you may want to be able to inspect the HTML at some point if later things run unexpected.
Now to the place you have the error with, at least we're getting closer:
$table = $dom->getElementsByTagName('table')->item(0);
foreach ($table->getElementsByTagName('tr') as $tr) {
# ...
Here is another problem not properly understanding or better fully taking care of all the return types of $dom->getElementsByTagName('table')->item(0);
.
And here's the news-flash: Two times:
- $dom->getElementsByTagName('table') may return null
- ->item(0) may return null
Both absolute correct return values by those methods. How to handle that in current PHP? Well, let the null pass through (null safe operator "?"):
$table = $dom->getElementsByTagName('table')?->item(0);
#
#- le magique question mark
And for the potential second null from item() return? Let's see:
foreach ($dom->getElementsByTagName('table')
?->item(0)
?->getElementsByTagName('tr') ?? [] as $tr) {
# ...
Combined with the null-coalescing operator "??", foreach an empty array (do not foreach) if acessing tr elements in a null-safe manner resulted in null, otherwise the happy path of the tr elements.
Now three sections later and a couple of tricks and perhaps new things here, but all are centering around handling return and input values. Be respectful to them and treat them also respectful when you pass them along, e.g. in form of an URL to a different service. You never know where these little errors creep in either on your side nor at someones' else computer.
Write the code in a fail-safe way, error out early.
(code in this answer is untested so far and for example purposes only. PHP 8 .0+ language level.)