Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

When I run var_dump($title), I get results really quick, but my script fetchs the whole page in order to get one result, then do it again, again. How can I do this job faster and optimize my code?

The following script works but it needs to be optimized because it works very slowly.

for ($j = 1; $j <= 1; $j++) { // number of pages I want to fetch, i.e., 1
    for ($i = 0; $i < 5; $i++) { // every page contains 20 records
        $site = "http://www.example.com/index.php?route=product/category&path=5_6&page=$j";         
        $ch = curl_init();
        $hc = "YahooSeeker-Testing/v3.9 (compatible; Mozilla 4.0; MSIE 5.5; Yahoo! Search - Web Search)";
        curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com');
        curl_setopt($ch, CURLOPT_URL, $site);
        curl_setopt($ch, CURLOPT_USERAGENT, $hc);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $site = curl_exec($ch);
        curl_close($ch);
        preg_match_all('@><a itemprop="url" href="http://www.example.com/id/(.*?)/(.*?).html&path=(.*?)"><span itemprop="name">(.*?)</span></a>@', $site, $title);

        $title[3][$i] = strip_tags($title[3][$i]); // strip tags
        $title[3][$i] = preg_replace('~.*?>~', '', $title[3][$i]);
        echo $title[3][$i]."<br>";
    }
}
share|improve this question

put on hold as unclear what you're asking by 200_success Feb 11 at 19:06

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit your question.

    
What is the purpose of this script? (I suspect that it doesn't do what you intend.) –  200_success Feb 11 at 18:55
    
The purpose of the script is to fetch the some titles from a target site. It works, but quite slowly, takes 20 seconds to get 20 records. I was wondering whether I use the right practices (for loop & curl_setopt) or not. –  salep Feb 11 at 18:57
    
Are you sure that this is working code? Why are you calling strip_tags() on the path parameter of the link URLs? If it is working code, then please add supporting detail to your question about the expected page content and intended output. –  200_success Feb 11 at 19:05
    
I was using file_get_contents first, then I decided to switch to curl and copied strip_tags & preg_replace from the old script. The code block I put here works perfectly fine and echoes the titles (this is the job), but very slowly. Here's an image : i.imgur.com/ZNGhazc.png –  salep Feb 11 at 19:14