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>";
}
}
strip_tags()
on thepath
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