PHP code example of beaplat / traffic

1. Go to this page and download the library: Download beaplat/traffic 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/ */

    

beaplat / traffic example snippets


//  一定要用 post 方法
//  一定要返回 succes 7个字符串
Route::post('traffic/callback', function () {
  $res = file_get_contents("php://input");
  Log::useFiles(storage_path('logs/traffic.log'));
  Log::info($res);
  return 'success';
});

// 优比格回调
Route::get('traffic/callback', function () {
  Log::useFiles(storage_path('logs/traffic.log'));
  Log::info(json_encode(app()->make('request')->all()));
  $result = [
    'code' => 1,
    'text' => 'success',
    'ext'  => []
  ];
  return response()->json($result);
});

// $trafficSize为流量大小,int,单位为M兆
Traffic::submit($mobile, $trafficSize);
// 举例
Traffic::submit('158xxxxxxxx', 10);

Traffic::balance();

Traffic::getCarrier($mobile);
// 举例
Traffic::getCarrier('158xxxxxxxx');

// 查询余额
Route::get('/traffic/balance', function () {
    try {
        return Traffic::balance();
    } catch (\Beaplat\Traffic\Exceptions\TrafficException $e) {
        return response()->json(['code' => $e->getCode(), 'message' => $e->getMessage()]);
    }
});

// 充值
Route::get('/traffic/submit', function () {
    try {
        $mobile = '158xxxxxxxx'; // 手机号
        $size = 10; // 流量大小 单位为M
        Traffic::submit($mobile, $size);
        return response()->json(['message' => '恭喜你充值成功,注意查收短信']);
    } catch (\Beaplat\Traffic\Exceptions\TrafficException $e) {
        return response()->json(['message' => $e->getMessage()]);
    }
});

php artisan vendor:publish --provider="Beaplat\Traffic\TrafficProvider"

php artisan migrate