sockets - PHP simple_html_dom and file_get_html in foreach loop. How to speed up parse time? -
i'm using php simple_html_dom , file_get_html in foreach loop loops 4,000 times.
here's i'm doing. i'm extracting content multiple urls on same host.
there 4,000+ urls on same host. example.com
example url: example.com/product.php?prod=00283
on next pass-thru of loop example.com/product.php?prod=03418
next pass-thru of loop example.com/product.php?prod=08192
etc, etc until 4000+ urls looped over.
tested 100 urls took on 1 , half minutes parse , output content.
is possible setup multithreaded approach in foreach loop bring contents of 4000 urls simultaneously?
in other words, use sockets or other kind of multithreaded approach iterate on urls in parallel?
here sample code below can see i'm doing here.
include 'simple_html_dom.php'; $partlist_file = 'parts.txt'; $partlist = file('partnumberlist.txt', file_ignore_new_lines | file_skip_empty_lines); $stock = ''; $isfirst = true; $firstline = 'sku,qty,visibility,is_in_stock' . "\n"; $output_first = ''; foreach($partlist $parts => $part) { $html = file_get_html('example.com/product.php?prod=' . $part); $ret = $html->find('span#cph_lb_stock_buy'); foreach($ret $element) { $stock = $element->plaintext; $stock = preg_replace(array('/\\n/','/\\r/'),'',$stock); $stock = trim($stock); if($stock == 'not in stock.') { $stock = '0,1,0'; } elseif($stock == 'in stock & ready ship!') { $stock = '4,4,4'; } if ($isfirst) { $output = $firstline; $isfirst = false; } $output .= 'k' . $part . ',' . $stock . "\n"; } } //print_r (trim($output,"\n")); file_put_contents($partlist_file, trim($output,"\n"));
Comments
Post a Comment