LOADING

标签 PHP 下的文章

流程
准备工作

  1. 注册google账号
  2. 创建结算账号
  3. 创建项目 https://console.cloud.google.com/projectselector2/iam-admin/serviceaccounts?hl=zh_CN
  4. 创建服务账号
  5. 创建服务账号的秘钥,下载到本地
  6. 设置有秘钥的服务账号资源权限
  7. composer require google/cloud-translate

代码

require 'vendor/autoload.php';

use Google\Cloud\Translate\V3\TranslationServiceClient;

$translationClient = new TranslationServiceClient();
$content = ['one', 'two', 'three']; //数组形式可传多个
$targetLanguage = 'es'; 目标语言
$response = $translationClient->translateText(
    $content,
    $targetLanguage,
    TranslationServiceClient::locationName('[PROJECT_ID]', 'global') // project_id是你的项目id,global是你秘钥的本地地址
);

foreach ($response->getTranslations() as $key => $translation) {
    $separator = $key === 2
        ? '!'
        : ', ';
    echo $translation->getTranslatedText() . $separator;
}

今天在压测接口时发现一个接口总是报Failed requests,看log发现请求成功了,最终发现问题的原因是因为
接口里面有随机从数据库中提取数据,导致的长度不一致,不用理会即可,如果还想测就暂时把随机取数据关闭

Failed requests:        132
   (Connect: 0, Receive: 0, Length: 132, Exceptions: 0)

测试时出现的Failed requests原因分析:
Failed requests: 2303
(Connect: 0, Length: 2303, Exceptions: 0)
只要出现Failed requests就会多一行数据来统计失败的原因,分别有Connect、Length、Exceptions。
Connect 无法送出要求、目标主机连接失败、要求的过程中被中断。
Length 响应的内容长度不一致 ( 以 Content-Length 头值为判断依据 )。
Exception 发生无法预期的错误。

array_multisort(array_column($redis,'praise_num'),SORT_DESC,$redis);

以redis中的praise_num 倒序排列

array_search($user_homework_id,array_column($redis,'id'));

查找user_homework_id 在redis中的key

1.修改laravel配置文件. configapp.php

   'log'=>'daily'

2.在项目目录中composer命令安装扩展:composer require arcanedev/log-viewer

3.安装成功后,需要在configapp.php 的providers阵列中注册服务

     Arcanedev\LogViewer\LogViewerServiceProvider::class

此时可以直接访问 http:://域名/log-viewer

需要改变route或者语言可以在log-viewer中的config配置

php artisan log-viewer:publish :运行此命令发布配置和翻译文件

git地址:https://github.com/ARCANEDEV/LogViewer/blob/master/_docs/1.Installation-and-Setup.md

需要注意 安装几个依赖, 最新版本互相可能不兼容, 选择最合适的就好

大日志会导致内存溢出, 建议小项目用

今天终于在项目中用到了goto特性

foreach ($coursewares as $key => $courseware) {

        st:
        $this_time = $new_times_tmp->where('c', $c)->first();
        if (!$this_time) {
            $c = $c + 1;
            $this_time = $new_times_tmp->where('c', $c)->first();
        }
        $new_times_tmp->where('c', $c)->first()->c = $this_time->c + 1;
        $day = $this->num_to_week($this_time->week);
        if($c==0){
            $begin_date = date('Y-m-d',strtotime('last '.$day));
        }else{
            $begin_date = date('Y-m-d',strtotime('+'.$c.' week last '.$day));
        }
        if ($festivals->where('festival_date', $begin_date)->first()) {
            goto st;
        }
        $new_cous->push([
            'cou_id'     => $courseware->id,
            'date'       => $begin_date,
            'begin_time' => $time_collect[$key]->begin_time,
            'end_time'   => $time_collect[$key]->end_time,
        ]);
    }

st:是声明一个作用域(只能在一个函数或类方法中使用),可以理解为标记,如果需要跳转到某一作用域时可以 用goto跳转
本来php作为解释语言是从上到下解释执行,有了goto就可以从下到上 从任意位置跳任意位置执行, 实在太方便了.

public function headingCode()

{
    list($msec, $sec) = explode(' ', microtime());
    $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    $msectime = substr(str_shuffle($msectime), -6);
    $banji    = Table::where('heading_code', $msectime)->first();
    if($banji) {
       $this->headingCode();
    }
    return $msectime;

}

这是一个递归算法能随机6位数,并且去数据库中查询是否已有,没有的话就返回
但是这里有个问题, 就是递归时实际上第一个方法的变量还在内存中没有销毁,第二次进来的时候虽然变量都叫一个名字,但是内存中分配的是不同的空间进行存储,所以随后会出现只返回第一次的$msectime,解决方法如下

public function headingCode()

{
    list($msec, $sec) = explode(' ', microtime());
    $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    $msectime = substr(str_shuffle($msectime), -6);
    $banji    = Banji::where('heading_code', $msectime)->first();
    if($banji) {
        $msectime = $this->headingCode();
    }
    return $msectime;

}