Reputation: 1
How can I make this script work with multi threads? Already tried all tutorials but without success :( And what is the maximum number threads I can use with curl php?
<?php $imput = file("$argv[1]"); $output = $argv[2]; foreach ($imput as $nr => $line) { $line = trim($line); print ("$nr - check :" . $line . "\r\n"); $check = ia_continutul($line); if (strpos($check,'wordpress') !== false) { $SaveFile = fopen($output, "a"); fwrite($SaveFile, "$line\r\n"); fclose($SaveFile); } } print "The END !\r\n"; function ia_continutul($url) { $ch = curl_init(); $timeout = 3; curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); curl_setopt($ch, CURLOPT_TIMEOUT, 5); $data = curl_exec($ch); curl_close($ch); return $data; } ?>
Upvotes: 0
Views: 2118
Reputation: 17148
You can multithread in PHP ...
class Check extends Thread {
public function __construct($url, $check){
$this->url = trim($url);
$this->check = $check;
}
public function run(){
if (($data = file_get_contents($this->url))) {
if (strpos($data, "wordpress") !== false) {
return $this->url;
}
}
}
}
$output = fopen("output.file", "w+");
$threads = array();
foreach( file("input.file") as $index => $line ){
$threads[$index]=new Check($line, "wordpress");
$threads[$index]->start();
}
foreach( $threads as $index => $thread ){
if( ($url = $threads[$index]->join()) ){
fprintf($output, "%s\n", $url);
}
}
https://github.com/krakjoe/pthreads
Upvotes: 4
Reputation: 420
You cannot multithread PHP. It is a scripting language, so the script is ran in a certain order and if you have to wait for a curl to finish it will keep loading while that happens, it is like putting a Sleep(1) function in your code.
There are some basic things you can do to help speed up your code. Do not do mysql request (I don't see any) inside a loop, instead build up a query then do it after the loop is over. Look at restructuring your code so you can do the minimum number of curls so it goes fast. Try to find a way to do the curl outside the loop.
Upvotes: -3