0

I need to speed up simple_xml_file part of the following code.

I was thinking multi threading ... Any ideas?

How could I speed up that part of the code where it get's the xml file.

<?php


$k = $_GET['key'];


echo "<ul id=\"List\">\n";
foreach ($feed as $url2):
$title=$url2['name'];
$url=$url2['url'];

if (strpos($url, $key)==true) {
//Load XML
$temp = simplexml_load_file("http://api.site.com/web?uri=$url"); //Need to speed this
//Echo
echo "<li>".$temp->filter->filterResponse->filteredText."</li>";
}else{
//URL Contains Nothing Check Title
if(strpos($title,$key)==true)
{

$temp = simplexml_load_file("http://api.site.com/web?uri=$url"); //Need to speed this
//Echo
echo "<li>".$temp->filter->filterResponse->filteredText."</li>";
}
}

endforeach;
echo "</ul>";

?>
4
  • Do you have any measurement of what's causing the slowness? The marked lines could be slow because the response is so big that it takes a lot of time to load, or can be that the remote server answers requests slowly... Commented Jul 8, 2013 at 13:17
  • I understand all that and it's not too slow either but what I'm after is making it as fast as it can be. The loop can go on 50 times, that's why i was thinking threading? Commented Jul 8, 2013 at 13:19
  • Threading isn't really what PHP do, however you could send the http requests paralell with the curl extension and then parse them all. Commented Jul 8, 2013 at 13:22
  • How would I integrate curl_multi_init into my code? Commented Jul 8, 2013 at 13:38

2 Answers 2

1

You could make the http requests for the API server run in parallel with curl, something like this:

//create the multiple cURL handle
$mh = curl_multi_init();
// gather curl handlers into an array for later use and add them to the multi request
$requests = array();
foreach ($feed as $url2) {
    $url=$url2['url'];
    $title=$url2['name'];

    // strpos can return 0 which will evaluate to false even if the needle is in fact in the beginning of the haystack
    if (strpos($url, $key) !== false || strpos($title,$key) !== false) {
        $ch = curl_init("http://api.site.com/web?uri=".$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_multi_add_handle($mh, $ch);
        $requests[] = $ch;
    }
}

// execute the requests
$running=null;
do {
    // exec the multy request, update the $running variable
    while (CURLM_CALL_MULTI_PERFORM === curl_multi_exec($mh, $running));
    // break the loop if we are done
    if (!$running) { 
        break;
    }
    // block until something interesting happens on a request
    while (($res = curl_multi_select($mh)) === 0);
    // if its an error, do something about it!
    if ($res === false) {
        // handle select errors here!
        break;
    }
    // repeat forever (or until we break out)
} while (true);

// loop trough the results
foreach ($requests as $ch) {
    // load resposne
    $xml = simplexml_load_string(curl_multi_getcontent($ch));

    // do your original processing
    echo "<li>".$xml->filter->filterResponse->filteredText."</li>";

    // clean up curl handler
    curl_multi_remove_handle($mh, $ch);
}
curl_multi_close($mh); 

I can't really test this without real data (apart from the syntax) but you get the idea. Your usage of strpos() looks strange to me, it might reutnr 0 (number zero) if the needle is in the beginning of the haystack but 0 == true evaulates to false, i'm not sure if this is you wanted.

Sign up to request clarification or add additional context in comments.

2 Comments

Just one more question...It runs very smoothly but for some reason I get blanks <li>'s from time to time in the listing ... any ideas of what would make that happen?
I don't really know, it could be that the response doesn't have the structure you are looking for, it could be that you are requesting the wrong things (the condition in the first loop wrong) or it could be that the responses are times out or returning some other error... I'm afraid you will have to debug it how those responses differ from the working ones.
1

PHP doesn't support multithreading, so there is not much you can do.
BTW. I think its much cleaner to join both conditions, since you perform the same actions anyway:

if (strpos($url, $key) == true OR strpos($title,$key) == true) {
   //Load XML
   $temp = simplexml_load_file("http://api.site.com/web?uri=$url"); //Need to speed this
   //Echo
   echo "<li>".$temp->filter->filterResponse->filteredText."</li>";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.