0

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;  
}
?>
2
  • php.net/manual/en/function.curl-multi-exec.php Commented Sep 13, 2012 at 14:35
  • 1
    PHP is not multithreaded, and will most likely never will be without a fundamental re-engineering of the language. Commented Sep 13, 2012 at 14:36

2 Answers 2

4

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

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

Comments

-3

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.

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.