首页
仓库
文档
nginx手册
Docker手册
workerman
Flask
PHP
python
RabbitMQ
其他
Linux
占位1
占位2
目录
###安装 composer require workerman/http-client ###DEMO <?php require __DIR__ . '/vendor/autoload.php'; use Workerman\Worker; $worker = new Worker(); $worker->onWorkerStart = function(){ $http = new Workerman\Http\Client(); $http->get('http://example.com/', function($response){ var_dump($response->getStatusCode()); echo $response->getBody(); }, function($exception){ echo $exception; }); $http->post('http://example.com/', ['key1'=>'value1','key2'=>'value2'], function($response){ var_dump($response->getStatusCode()); echo $response->getBody(); }, function($exception){ echo $exception; }); $http->request('http://example.com/', [ 'method' => 'POST', 'version' => '1.1', 'headers' => ['Connection' => 'keep-alive'], 'data' => ['key1' => 'value1', 'key2'=>'value2'], 'success' => function ($response) { echo $response->getBody(); }, 'error' => function ($exception) { echo $exception; } ]); }; Worker::runAll(); ###GET <?php require 'vendor/autoload.php'; //自动加载 use Workerman\Timer; use Workerman\Worker; Worker::$daemonize = true; // 如果Worker::$daemonize = true 所有的打印输出全部保存在workerman.txt文件中 Worker::$logFile = './shenyang_dingwei_caiji.txt'; Worker::$stdoutFile = './log/shenyang_dingwei_caiji.txt'; //$userdata=[]; //多进程之间 无法共享数据 所有全局变量 无效 改用 GlobalData // 创建一个Worker监听2345端口,使用http协议通讯 $worker = new Worker("tcp://0.0.0.0:30040"); //设置当前Worker实例的名称,方便运行status命令时识别进程。不设置时默认为none。 $worker->name = 'shenyang_dingwei_caiji'; // 启动1个进程对外提供服务 $worker->count = 1; //设置Worker子进程启动时的回调函数,每个子进程启动时都会执行。 $worker->onWorkerStart = function($worker) { // 将db实例存储在全局变量中(也可以存储在某类的静态成员中) // global $db; // $db = new \Workerman\MySQL\Connection('127.0.0.1', '3306', 'cloud', '123123', 'cloud'); $db=1; echo "开启进程 ID:{$worker->id}\n"; $http = new Workerman\Http\Client(); Timer::add(60, function() use($db,$worker,$http) { echo "开始推定位!\n"; $http->get('http://123.123.123.123:123123/api/123/666', function($response){ //var_dump($response->getStatusCode()); echo $response->getBody()."\n"; }, function($exception){ echo $exception; }); }); }; // 运行所有Worker实例。 //Worker::runAll()执行后将永久阻塞,也就是说位于Worker::runAll()后面的代码将不会被执行。所有Worker实例化应该都在Worker::runAll()前进行。 Worker::runAll(); ###POST <?php require 'vendor/autoload.php'; //自动加载 use Workerman\Timer; use Workerman\Worker; //Worker::$daemonize = true; // 如果Worker::$daemonize = true 所有的打印输出全部保存在workerman.txt文件中 Worker::$logFile = './jiekoutuisongrenwu.txt'; Worker::$stdoutFile = './log/jiekoutuisongrenwu.txt'; //$userdata=[]; //多进程之间 无法共享数据 所有全局变量 无效 改用 GlobalData // 创建一个Worker监听2345端口,使用http协议通讯 $worker = new Worker("tcp://0.0.0.0:30050"); //设置当前Worker实例的名称,方便运行status命令时识别进程。不设置时默认为none。 $worker->name = 'jiekoutuisongrenwu'; // 启动1个进程对外提供服务 $worker->count = 1; //设置Worker子进程启动时的回调函数,每个子进程启动时都会执行。 $worker->onWorkerStart = function($worker) { // 将db实例存储在全局变量中(也可以存储在某类的静态成员中) // global $db; $db = new \Workerman\MySQL\Connection('127.0.0.1', '3306', 'cloud', '123123', 'cloud'); echo "开启进程 ID:{$worker->id}\n"; $http = new Workerman\Http\Client(); Timer::add(5, function() use($db,$worker,$http) { echo "开始接口!\n"; $new = $db->row("SELECT * FROM `推送接口任务` where status=0 order by id desc limit 1"); if (!empty($new)) { $http->post($url,['key1'=>'value1','key2'=>'value2'], function($response){ echo $response->getStatusCode(); if($response->getStatusCode()=='200'){ echo $response->getBody()."\n"; $resdata=$response->getBody(); $restime=date('Y-m-d G:i:s'); $db->query("UPDATE `推送接口任务` SET `status` = '1',restime='$restime',resdata='$resdata' WHERE ID=".$new['id']); } }, function($exception){ echo $exception; }); } }); }; // 运行所有Worker实例。 //Worker::runAll()执行后将永久阻塞,也就是说位于Worker::runAll()后面的代码将不会被执行。所有Worker实例化应该都在Worker::runAll()前进行。 Worker::runAll(); ###POST json <?php require 'vendor/autoload.php'; //自动加载 use Workerman\Timer; use Workerman\Worker; Worker::$daemonize = true; // 如果Worker::$daemonize = true 所有的打印输出全部保存在workerman.txt文件中 Worker::$logFile = './jiekoutuisongrenwu.txt'; Worker::$stdoutFile = './log/jiekoutuisongrenwu.txt'; //$userdata=[]; //多进程之间 无法共享数据 所有全局变量 无效 改用 GlobalData // 创建一个Worker监听2345端口,使用http协议通讯 $worker = new Worker("tcp://0.0.0.0:30050"); //设置当前Worker实例的名称,方便运行status命令时识别进程。不设置时默认为none。 $worker->name = 'jiekoutuisongrenwu'; // 启动1个进程对外提供服务 $worker->count = 1; //设置Worker子进程启动时的回调函数,每个子进程启动时都会执行。 $worker->onWorkerStart = function($worker) { // 将db实例存储在全局变量中(也可以存储在某类的静态成员中) // global $db; $db = new \Workerman\MySQL\Connection('127.0.0.1', '3306', 'cloud', '123123', 'cloud'); echo "开启进程 ID:{$worker->id}\n"; $http = new Workerman\Http\Client(); Timer::add(1, function() use($db,$worker,$http) { echo "开始接口!\n"; $new = $db->row("SELECT * FROM `推送接口任务` where status=0 order by id desc limit 1"); if (!empty($new)) { $http->request($new['url'], [ 'method' => 'POST', 'version' => '1.1', 'headers' => ['Connection' => 'keep-alive','content-type'=>'application/json'], 'data' => $new['postdata'], 'success' => function ($response) use($new,$db) { echo 'getStatusCode:'.$response->getStatusCode(); if($response->getStatusCode()=='200'){ echo $response->getBody()."\n"; $resdata=$response->getBody(); $restime=date('Y-m-d G:i:s'); $db->query("UPDATE `推送接口任务` SET `status` = '1',restime='$restime',resdata='$resdata' WHERE id=".$new['id']); } }, 'error' => function ($exception) { echo $exception; } ]); } }); }; // 运行所有Worker实例。 //Worker::runAll()执行后将永久阻塞,也就是说位于Worker::runAll()后面的代码将不会被执行。所有Worker实例化应该都在Worker::runAll()前进行。 Worker::runAll(); ###定时执行http composer require workerman/workerman composer require workerman/mysql composer require workerman/http-client <?php require 'vendor/autoload.php'; //自动加载 use Workerman\Timer; use Workerman\Worker; // 如果Worker::$daemonize = true 所有的打印输出全部保存在workerman.txt文件中 Worker::$logFile = './dingshiqi.txt'; // 创建一个Worker监听2345端口,使用http协议通讯 $worker = new Worker("tcp://0.0.0.0:30000"); //设置当前Worker实例的名称,方便运行status命令时识别进程。不设置时默认为none。 $worker->name = 'dingshiqi'; // 启动1个进程对外提供服务 $worker->count = 1; //设置Worker子进程启动时的回调函数,每个子进程启动时都会执行。 $worker->onWorkerStart = function($worker) { // 将db实例存储在全局变量中(也可以存储在某类的静态成员中) global $db; $db = new \Workerman\MySQL\Connection('127.0.0.1', '3306', 'cloud', '123456', 'cloud'); echo "开启进程 ID:{$worker->id}\n"; $http = new Workerman\Http\Client(); Timer::add(5, function() use($db,$worker,$http) { echo "开始定位采集任务!\n"; $k=10; for($i=0;$i<$k;$i++){ $http->get('http://123.123.123.123:3333/test.php?str=123', function($response){ // var_dump($response->getStatusCode()); echo $response->getBody()."\n"; }, function($exception){ echo $exception; }); } }); }; // 运行所有Worker实例。 Worker::runAll();