John Davidson

php - Why using DOMDocument makes site load slower?

0 comments
Message:


I'm using DOMDocument with xpath to load some data to my site from external (fast) website.


Right now I use 4 urls (please see below). I need to increase to 8 urls.


What I have noticed, that the more of those you add, the more slower the site loads.


Is there any way to use xpath for more faster load?


Or maybe there's at least some kind of a way to load the data on website1 (child website) and when it loads, include the data to my main website.


Any tips would be appeciated.


<?php
$parent_title = get_the_title( $post->post_parent );
$html_string = file_get_contents('weburladresshere');
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html_string);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
$values = array();
$row = $xpath->query('myquery');
foreach($row as $value) {
print($value->nodeValue);
}

?>


My Answer:
Using DOMDocument and XPath to load data from external websites can slow down your site because each time you make a request to an external website, it takes time to fetch the data and parse it using DOMDocument. Additionally, making multiple requests to different external websites can further slow down your site due to the increased number of requests and processing time.

One way to potentially speed up the loading time is to cache the data retrieved from the external websites. You can store the data in a local cache and only make requests to the external websites when the data in the cache is outdated or expired. This can help reduce the number of requests and speed up the loading time of your site.

Another approach is to use asynchronous requests to fetch the data from multiple external websites simultaneously. This can help reduce the overall loading time by fetching the data in parallel instead of sequentially.

Alternatively, you can consider preloading the data on the child website and then including the preloaded data on your main website. This can help reduce the loading time on your main website as the data is already available and does not need to be fetched dynamically.

Overall, optimizing the way you fetch and process data from external websites can help improve the loading time of your site. Experiment with different approaches and techniques to find the best solution for your specific use case.

Rate this post

3 of 5 based on 3193 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us