分类 php 下的文章

第一步:
登录后台,点击设置-》永久链接-》选择启用重写功能,(注:如果提示启用功能检查失败,根据提示勾选确认即可
第二步:
登录服务器,修改nginx配置文件,在location中填写一下内容,

       location / {
                index index.html index.php;
                if (-f $request_filename/index.html) {
                        rewrite (.*) $1/index.html break;
                }
                if (-f $request_filename/index.php) {
                        rewrite (.*) $1/index.php;
                }
                if (!-f $request_filename) {
                        rewrite (.*) /index.php;
                }
        }
  

第三步:重启nginx服务并验证网站url

sudo /usr/local/nginx/sbin/nginx -s reload

验证没有问题则配置通过

今天在做项目开发时,接口报了:Cannot redeclare xxx 错误,第一反应是方法被重复定义,估计这个方法被其他人在公共文件中定义了,找了一下,果然,方法名一样,由于返回内容不一样,所以对自己的方法进行重命名,错误得到解决,log一下

laravel任务调度在App\Console\Kernel类的 schedule 方法中定义所有的调度任务,以Artisan命令调度为例:
1、在schedule添加调度命令及时间

protected function schedule(Schedule $schedule)
{
        // $schedule->command('inspire')
        //          ->hourly();
        $schedule->command("my:test")->everyMinute();//每分钟执行
}

2、创建调度文件,该文件存储于App\Console\Commands目录下:

php artisan make:command MyTest

3、修改调度,定义任务调度名称和描述

protected $signature = 'my:test';
protected $description = 'This is my test';

    public function handle()
    {
        //自定义任务
        dump("hello world") ;
    }

4、执行测试,输出"hello world"

php artisan my:test

注意,如果你不知道如何向服务器添加 cron 配置项,请考虑使用 Laravel Forge 之类的服务来为你管理 cron 配置项:

* * * * * cd /你的项目路径 && php artisan schedule:run >> /dev/null 2>&1

常用时间说明
->everyMinute(); 每分钟执行一次任务
->hourly(); 每小时执行一次任务
->daily(); 每天 00:00 执行一次任务
->dailyAt('13:00'); 每天 13:00 执行一次任务

更多调度请参考文档:https://learnku.com/docs/laravel/9.x/scheduling/12238#105d37

最近业务需要,需要统计每天、每周、每月、每季度、上半年、全年等数据,所以找到找到了Carbon插件,可以方便的获取对应时间范围,例如:

today 根据当前时间创建Carbon实例,时间重置为 0时0分0秒

startOfWeek 重置当前实例时间为 本周的第一天,同时设置 0时0分0秒

endOfWeek 重置当前实例时间为 本周的最后一天,同时设置 23时59分59秒

startOfMonth 重置当前实例时间为 本月第一天,同时设置 0时0分0秒

endOfMonth 重置当前实例时间为 本月最后一天,同时设置 23时59分59秒

...

Carbon实例详细参考:
https://www.jianshu.com/p/7e45bc4d0006

当前示例是展示curl请求raw数据的方法,如下:

$headers = [
    'Content-Type:application/json',
    'App-Key:'.$key,
    'App-Sign:'.$result,
    'X-AjaxPro-Method:ShowList',
    'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36'
];

$callBack = [
    "mobile" => 111,
    "content" => "测试"
];

$postUrl= 'http://www.baidu.com/';

$jsonContent = json_encode($callBack);

//用法
$result = http_post_json($postUrl, $headers, $jsonContent);

/* @param $postUrl
 * @param $headers
 * @param $jsonContent
 * @return mixed
 */
function http_post_json($postUrl, $headers, $jsonContent)
{
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//执行命令
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonContent);
    $data = curl_exec($ch);//运行curl
    curl_close($ch);
    $data = json_decode($data,true);
    return $data;
}