php slim 命令行(cli)运行

只需要mock环境变量即可

$settings = require __DIR__ . '/../src/settings.php';
if (PHP_SAPI == 'cli') {
    global $argv;
    $env = [];
    if (isset($argv[2])) {
        $env['REQUEST_METHOD'] = $argv[2];
    }
    $env['REQUEST_URI'] = $argv[1];
    $settings['environment'] = \Slim\Http\Environment::mock($env);
}
$app = new App($settings);

命令行的第一个参数就是uri,第二个参数是method

PHP各版本XHProf

扩展

<php 7
https://pecl.php.net/package/xhprof
https://github.com/phacility/xhprof/tree/master/extension

>=php 7
https://github.com/tideways/php-profiler-extension (https://tideways.io/profiler/xhprof-for-php7-php5.6)
https://github.com/longxinH/xhprof

UI

https://github.com/phacility/xhprof

composer

https://github.com/PBWebMedia/xhprof

php.ini

auto_prepend_file = xhprof.php
xhprof.output_dir = /path/to/xhprof/run
tideways版本参考官方文档

以太坊web3部署新合约(contract)

用nodejs实现,web3版本1.0.0beta,都是坑,直接上代码

#!/usr/bin/env node
//export NODE_PATH="/usr/lib/node_modules"
const Web3 = require('web3');
const Tx = require('ethereumjs-tx')
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('https://rinkeby.infura.io/...'));
var privateKey = new Buffer('...', 'hex')
//abi和bytecode通过solidity编译器命令行获取
var abi = JSON.parse('...');
var contract = new web3.eth.Contract(abi)
var bytecode = '0x...';
//钱包地址
var from = '0x...';
web3.eth.getGasPrice().then(function(gasPrice){
    const gasPriceHex = web3.utils.toHex(gasPrice);
    const gasLimitHex = web3.utils.toHex(1000000);
    web3.eth.getTransactionCount(from,function(err,nonce){
        const nonceHex = web3.utils.toHex(nonce);
        const t = {
            nonce: nonceHex,
            gasPrice: gasPriceHex,
            gasLimit: gasLimitHex,
            data: bytecode,
            from: from
        };
        var tx = new Tx(t);
        tx.sign(privateKey);
        var serializedTx = '0x' + tx.serialize().toString('hex');
        web3.eth.sendSignedTransaction(serializedTx,
        function(err, r) {
            if(!err){
                web3.eth.getTransaction(r,
                function(err, r) {
                    console.log(r)
                })
            }
        })
    })
})

gas数量自行评估,可以用mist先看一下。