Exemple #1
0
function getJuheapiProxy($proxy)
{
    $url = "http://japi.juheapi.com/japi/fatch?key=68d6e9f24d53a22e0344b70d72c75462&v=1.0&pkg";
    if (!is_file($proxy)) {
        $curl = `curl -s "{$url}"`;
        $curl = json_decode($curl, true);
        if (!isset($curl["error_code"]) || $curl["error_code"] > 0) {
            loger("Loading proxy fail");
            return false;
        } else {
            file_put_contents($proxy, join("\n", $curl["result"]));
        }
    }
}
/**
 * @var array $config
 * @param array $fieldsData
 */
function send_to_app(array $fieldsData)
{
    $config_file = APPROOT . '/config/config.php';
    if (!is_file($config_file)) {
        loger_error("Line: " . __LINE__ . "; Not found file config.php");
        exit;
    }
    $config = (include $config_file);
    $url = $config['site_url'] . 'index.php/apps/owncollab_talks/parse_manager';
    $fcount = $fieldsData['files_count'];
    $parserMessage = $fieldsData['parser_message'];
    if ($fcount > 0) {
        $fieldsData['files'] = files_parser($parserMessage, $fieldsData);
    }
    unset($fieldsData['parser_message']);
    $fieldsData['mail_domain'] = $config['mail_domain'];
    $fieldsData['site_url'] = $config['site_url'];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fieldsData));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    // console info
    //print_r($result);
    if ($error) {
        loger_error("Line: " . __LINE__ . "; cURL request fail! Error: " . $error);
        exit;
    }
    try {
        $resultData = json_decode($result, true);
        if ($resultData['type'] != 'ok') {
            loger_error("Line: " . __LINE__ . "; Result from server is bad! QueryData:" . $result);
        } else {
            loger("Parse and send mail data is success! From: " . $fieldsData['from'] . " To: " . $fieldsData['to'] . " Result data: " . $result);
        }
    } catch (Exception $e) {
        loger_error("Line: " . __LINE__ . "; Result from server not decode! QueryData:" . $result);
    }
}
Exemple #3
0
    `rm -f {$dir}`;
    //如何实现异步多线程
    foreach ($task as $job => $tasks) {
        foreach ($tasks as $url) {
            //进程上限锁
            while (thread_count() > 10) {
                sleep(1);
            }
            $pid = pcntl_fork();
            //父进程和子进程都会执行下面代码
            if ($pid == -1) {
                //错误处理:创建子进程失败时返回-1.
                loger("Could not fork new thread !");
                exit(1);
            } else {
                if ($pid) {
                    //父进程会得到子进程号,所以这里是父进程执行的逻辑
                    pcntl_wait($status, WNOHANG);
                    //等待子进程中断,防止子进程成为僵尸进程。
                } else {
                    //子进程得到的$pid为0, 所以这里是子进程执行的逻辑。
                    $return = runtask($url, $proxy);
                    loger($url . "->" . substr($return, 0, strpos($return, "\n")));
                    //子进程完成一定要结束掉进程,否则进程会越来越多
                    exit;
                }
            }
        }
    }
}
exit;