PHP code example of lobtao / tp5helper

1. Go to this page and download the library: Download lobtao/tp5helper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lobtao / tp5helper example snippets


use lobtao\tp5helper\Curl;
$curl = new Curl();

//get http://example.com/
$response = $curl->get('http://example.com/');

if ($curl->errorCode === null) {
   echo $response;
} else {
     // List of curl error codes here https://curl.haxx.se/libcurl/c/libcurl-errors.html
    switch ($curl->errorCode) {

        case 6:
            //host unknown example
            break;
    }
}

// GET request with GET params
// http://example.com/?key=value&scondKey=secondValue
$curl = new Curl();
$response = $curl->setGetParams([
        'key' => 'value',
        'secondKey' => 'secondValue'
     ])
     ->get('http://example.com/');

// POST URL form-urlencoded
$curl = new Curl();
$response = $curl->setPostParams([
        'key' => 'value',
        'secondKey' => 'secondValue'
     ])
     ->post('http://example.com/');

// POST with special headers
$curl = new Curl();
$response = $curl->setPostParams([
        'key' => 'value',
        'secondKey' => 'secondValue'
     ])
     ->setHeaders([
        'Custom-Header' => 'user-b'
     ])
     ->post('http://example.com/');

// POST JSON with body string & special headers
$curl = new Curl();

$params = [
    'key' => 'value',
    'secondKey' => 'secondValue'
];

$response = $curl->setRequestBody(json_encode($params))
     ->setHeaders([
        'Content-Type' => 'application/json',
        'Content-Length' => strlen(json_encode($params))
     ])
     ->post('http://example.com/');

// Avanced POST request with curl options & error handling
$curl = new Curl();

$params = [
    'key' => 'value',
    'secondKey' => 'secondValue'
];

$response = $curl->setRequestBody(json_encode($params))
     ->setOption(CURLOPT_ENCODING, 'gzip')
     ->post('http://example.com/');

// List of status codes here http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
switch ($curl->responseCode) {

    case 'timeout':
        //timeout error logic here
        break;

    case 200:
        //success logic here
        break;

    case 404:
        //404 Error logic here
        break;
}

//list response headers
var_dump($curl->responseHeaders);

namespace app\index\controller;

use lobtao\tp5helper\RpcController;
use lobtao\tp5helper\RpcException;
use think\Session;

class ServiceController extends RpcController {

    function index() {
        return $this->handle('app\\service\\', function ($func, $params) {
            if (in_array(strtolower($func), ['user_login', 'user_logout'])) //登录方法不判断
                return;

            if(!Session::get('user')){
                throw new RpcException('尚未登录,不能访问');
            }
        });
    }
}

namespace app\service;


use think\Session;

class UserService {
    function login($params){
        Session::set('user', ['name'=>'远思']);
    }

    function logout($params){
        Session::delete('user');
        Session::destroy();
    }

    function test(){
        return '恭喜,你可以正常访问此方法';
    }
}

/**
 * Created by lobtao.
 * User: Administrator
 * Date: 2017-7-26
 * Time: 16:51
 * workerman的性能是apache的239倍
 */

namespace app\admin\command;

use lobtao\tp5helper\WorkerRpc;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http;
use Workerman\Worker;

class Api extends Command
{
    protected function configure() {
        $this->setName('api')
            ->addArgument('args')
            ->addArgument('daemon')
            ->setDescription('api接口调用');
    }

    protected function execute(Input $input, Output $output) {
        global $argv;
        array_shift($argv);//弹出第一个参数
        if ($argv[1] == 'startd') {
            $argv[1] = 'start';
            $argv[2] = '-d';
        }

        $worker->onMessage = function (TcpConnection $con, $data) {
            if($data['server']['REQUEST_URI'] == '/favicon.ico') return;//忽略favicon.ico请求
            Http::header('Access-Control-Allow-Origin:*');//允许前端跨域请求
            $rpc = new WorkerRpc();
            $rpc->handle($con, 'app\\service\\');
        };
        Worker::runAll();
    }
}