Need Help Optimizing PHP script pulling Site Name of test sites

404

We have around 20 test sites that have various wordpress projects and I have a working script to pull just the name of the blog and add it to an internal site we use to store a bunch of information.

<?php
libxml_use_internal_errors(true);
$c = file_get_contents("http://test1.company.com");
$d = new DomDocument();
$d->loadHTML($c);
$xp = new domxpath($d);
foreach ($xp->query("//meta[@property='og:site_name']") as $el) {
echo $el->getAttribute("content");
}
?>

It works just fine the way I have it set up it is just very slow when I have it pulling info from all 20 sites. I was wondering if theres a way to optimize how its pulling the information?

960

Answer

Solution:

Loading the document withnew DomDocument will be slow. Imagine you're telling someone to memorize a book for you and all you really care about is the title of the page. You're making them go through a lot of work for something you can already get the answer to.

When you receive the contents as a raw string, run apreg_match on it to pull the value, should be considerably faster especially since you know it's part of theog:site_name meta tag.

I hope this helps!

Update

I would also take it a step further and only retrieve the first 1000 characters so you're not pulling in the entire document. Since you're doing a regular expression search on it you can bypass all the XML parsing.

If you need to get more information, then you're probably stuck pulling the entire document.

Here's an example of getting the first 1000 characters:

file_get_contents('./people.txt', NULL, NULL, 0, 1000);

The last two arguments areoffset andmax length. You want to start at the first character (0) and get1000 bytes/characters only.

People are also looking for solutions to the problem: php - Storing filesize in database vs calling filesize()

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.