/** * Static method to create your threads from functions ... **/ public static function call($method, $params) { $thread = new Async($method, $params); if ($thread->start()) { return $thread; } /** else throw Nastyness **/ } /** * Do whatever, result stored in $this->result, don't try to join twice **/ public function __toString() { if (!$this->joined) { $this->joined = true; $this->join(); } return $this->result; } } $future = Async::call("file_get_contents", array("https://www.rohlik.cz/c75533-napoje?paginator-page=14&do=paginator-loadMore")); /* here's us counting the bytes out, note, __toString() magic joined so no need to join explicitly */ printf("Got %d bytes from php.net\n", strlen((string) $future)); /* you can reference again as a string because you cached the result, YOU CANNOT JOIN TWICE */ printf("First 16 chars: %s\n", substr((string) $future, 0, 16)); /* if you have no __toString(): */ $response = $future->result; var_dump($response);
**/ public static function call($method, $params) { $thread = new Async($method, $params); if ($thread->start()) { return $thread; } /** else throw Nastyness **/ } /** * You could join the result, or if you know the response is a string then do the following: **/ public function __toString() { if (!$this->result) { $this->result = $this->join(); } return (string) $this->result; } } /* here's us calling file_get_contents in a thread of it's own */ $future = Async::call("file_get_contents", array("http://www.php.net")); /* here's us counting the bytes out, note, __toString() magic joined so no need to join explicitly */ printf("Got %d bytes from php.net\n", strlen((string) $future)); /* you can reference again as a string because you cached the result, YOU CANNOT JOIN TWICE */ printf("First 16 chars: %s\n", substr((string) $future, 0, 16)); /* if you have no __toString(): */ /* $response = $future->join(); */ /* you could also not use a reference to the thread, if the threads job was to say, log stats and you do not need to know when or how it returns, then you could just Async::call at the beginning of the request and by the end it would be finished */
public function LoadSteam($apiKey) { $this->Timers['Steam'] = MicroTime(true); $steamSearchPlayers = array_chunk($this->Players, 100); $builder = ''; $chunks = array(); $c = 0; foreach ($steamSearchPlayers as $key => $value) { foreach ($value as $k => $v) { $builder .= $v->SteamId; if (end($value) != $v) { $builder .= ','; } elseif (end($value) == $v) { $chunks[] = $builder; $builder = ''; } } } $presponses = array(); $bresponses = array(); foreach ($chunks as $key => $value) { if (empty($value)) { continue; } // $curl = curl_init(); $baseUrl = 'https://api.steampowered.com/'; // var_dump($baseUrl."ISteamUser/GetPlayerSummaries/v0002/?key=$apiKey&steamids=$builder"); // exit; // curl_setopt_array($curl, array( // CURLOPT_RETURNTRANSFER => 1, // CURLOPT_URL => $baseUrl."ISteamUser/GetPlayerSummaries/v0002/?key=$apiKey&steamids=$value", // CURLOPT_SSL_VERIFYPEER => false, // )); // $profiles = curl_exec($curl); $profiles = Async::call('file_get_contents', array($baseUrl . "ISteamUser/GetPlayerSummaries/v0002/?key={$apiKey}&steamids={$value}")); $presponses = array_merge($presponses, json_decode((string) $profiles)->response->players); // if (!$profiles) { // die('Error: "'.curl_error($curl).'" - Code: '.curl_errno($curl)); // } // curl_close($curl); // $curl = curl_init(); // curl_setopt_array($curl, array( // CURLOPT_URL => $baseUrl."ISteamUser/GetPlayerBans/v1/?key=$apiKey&steamids=$value", // CURLOPT_RETURNTRANSFER => 1, // CURLOPT_SSL_VERIFYPEER => false, // )); // $bans = curl_exec($curl); $bans = Async::call('file_get_contents', array($baseUrl . "ISteamUser/GetPlayerBans/v1/?key={$apiKey}&steamids={$value}")); $bresponses = array_merge($bresponses, json_decode((string) $bans)->players); // if (!$bans) { // die('Error: "'.curl_error($curl).'" - Code: '.curl_errno($curl)); // } // curl_close($curl); } $this->LinkSteamProfiles($presponses); $this->LinkSteamBans($bresponses); $this->SteamLoaded = true; $this->Timers['Steam'] = Number_Format(MicroTime(true) - $this->Timers['Steam'], 4, '.', ''); }