emoji
utf8 编码问题1
2
3
4
5
6
7
8
9
10
11 function nickname_encode($s){
$chars = preg_split('//u', $s, null, PREG_SPLIT_NO_EMPTY);
foreach ($chars as &$c) {
if (strlen($c) > 3 || $c === "%"){
$c = urlencode($c);
}
}
return implode($chars);
}
解码直接用urldecode就行了。
strtotime
1 | var_dump(date("Y-m-d", strtotime("2017-06-31"))); |
三元运算符
1 | 运算符可 用于判断$a变量不存在的情况(也可用于数组),而使用三元运算符判断一个未定义的变量,PHP会抛出异常。也正是因为这样,用??判断一个赋值为0的变量的时候结果是不一样的。https://laravel-china.org/articles/17304?#reply64712 |
Error while reading line from the server. [tcp://127.0.0.1:6379]
redis 加了代理 Twemproxy,而 predis 对这个配置默认执行 select 操作,导致了连接错误。
大家以后要注意,如果 redis 有代理的话,别忘了把 'database' => 0,
这个配置删掉。
https://laravel-china.org/topics/6774/meet-a-pit-of-laravel-redis-share
PHP empty 函数判断结果为空
1 | class person |
Laravel Redis 多个进程同时取队列问题
Laravel 使用 Redis 的 list 作为队列的数据结构,并会为每个队列分配一个 ID
Laravel 中入队列方法1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public function pushRaw($payload, $queue = null, array $options = [])
{
$this->getConnection()->rpush($this->getQueue($queue), $payload);
return Arr::get(json_decode($payload, true), 'id');
}
用的是 Redis 的 rpush 命令。
Laravel 中取队列方法
public function pop($queue = null)
{
$original = $queue ?: $this->default;
$queue = $this->getQueue($queue);
$this->migrateExpiredJobs($queue.':delayed', $queue);
if (! is_null($this->expire)) {
$this->migrateExpiredJobs($queue.':reserved', $queue);
}
list($job, $reserved) = $this->getConnection()->eval(
LuaScripts::pop(), 2, $queue, $queue.':reserved', $this->getTime() + $this->expire
);
if ($reserved) {
return new RedisJob($this->container, $this, $job, $reserved, $original);
}
}
这里用的是 lua 脚本取队列,如下:
public static function pop()
{
return <<<'LUA'
local job = redis.call('lpop', KEYS[1])
local reserved = false
if(job ~= false) then
reserved = cjson.decode(job)
reserved['attempts'] = reserved['attempts'] + 1
reserved = cjson.encode(reserved)
redis.call('zadd', KEYS[2], ARGV[1], reserved)
end
return {job, reserved}
LUA;
}
那么结论是:从 Laravel 的处理方式和打印的日志结果看,即使多个进程读取同一个队列,也不会读取到一样的数据。
队列执行失败之后根本不会执行failed方法
执行队列侦听器时加上 –tries参数即可,failed()方法只有在重试指定次数完成后才会调用failed()方法如:
http://blog.zhouchenxi.cn/post/51.html
php artisan queue:listen –queue=default,listeners –tries=3
顺便提一下:如果指定了队列名称必须在侦听器的参数上加上 –queue参数才行,不然没有指定的队列是不会运行的。
event 指定队列1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 public $queue = 'listeners';
public function queue($queue, $job, $data)
{
if (isset($this->queue, $this->delay)) {
return $queue->laterOn($this->queue, $this->delay, $job, $data);
}
if (isset($this->queue)) {
return $queue->pushOn($this->queue, $job, $data);
}
if (isset($this->delay)) {
return $queue->later($this->delay, $job, $data);
}
return $queue->push($job, $data);
}
php7不支持 preg_replace e修正符
车轮升级PHP7踩过的一些坑 http://php.swoole.com/wiki/%E8%BD%A6%E8%BD%AE%E5%8D%87%E7%BA%A7PHP7%E8%B8%A9%E8%BF%87%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91
php5 php7不兼容的地方:https://segmentfault.com/a/1190000013837897
1 | $joinStr = preg_replace("/__([A-Z_-]+)__/e",$prex.".strtolower('$1')",$joinStr); |
内存溢出
1 | //https://laravel-china.org/articles/16312/handling-large-amounts-of-data-to-write-files-to-avoid-memory-overflow-and-response-timeout |
git bash 输入python命令停滞
$ python -i
$ winpty python -3
保留两位小数
round(a,2)
r = lambda f: f - f % 0.01
int(a*100)/100
print(“%.2f” %a)
后期绑定
1 | //https://www.v2ex.com/t/482659#reply8 |
{} + []
1 | ({}).valueOf() // [] |
echo 后发送 header
使用 fastcgi_finish_request();( PHP-fpm )可以强制断开客户端连接 session 本质上是 header set-cookie,所以不算输出。这里的输出指的是 response body 的部分
https://www.v2ex.com/t/481127#reply16
isset在php5.6-和php7.0+的一些差异
1 | https://segmentfault.com/a/1190000016097997 |
laravel 中使用 导出 excel 时报错
PHPExcel_Calculation_Exception: Q5!A65 → Formula Error: An unexpected error occured in /application/www/web_git-pull/vendor/phpoffice/phpexcel/Classes/PHPExcel/Cell.php:291
原因:
在excel中一个单元格如果是以=
开头,则说明这个单元格是根据其他单元格的值算出来的,=
后面必须跟着一个合法的表达式,而那个字符串是用户的输入,很明显不应该是一个合法的表达式,所以应该在代码中过滤掉
解决:$str = "\t".$str;
//同时解决 如 11111111111 显示成 1.11111E+29
参考
https://github.com/Maatwebsite/Laravel-Excel/issues/506
https://stackoverflow.com/questions/11189145/formula-error-in-phpexcel
gzip UnicodeDecodeError
1
2
3
4
5
6
7
8
9import urllib.request
import gzip
# 注意要先读取内容才可以解压缩
data = urllib.request.urlopen(url).read()
try:
html =data.decode("utf-8")
except:
html =gzip.decompress(data).decode("utf-8")
Intervention/Image 图片写入中文
1 | https://laravel-china.org/topics/13642/problems-encountered-in-writing-chinese-with-interventionimage-pictures-and-solutions |
获取某字段修改前的值
1 | Issue::saving(function(Issue $issue){ |
前一天
1 | https://www.cnblogs.com/lhm166/articles/6066762.html |
phpstorm xdebug
1 | https://www.cnblogs.com/baocheng/p/5775938.html |
Python for 循环中的陷阱
https://www.v2ex.com/t/470443#reply38
pyinstaller 打包 exe
pyinstaller -p venv_path (中间一定要有空格) you.py https://www.v2ex.com/t/476247#reply25
http://ju.outofmemory.cn/entry/137370
php捕获fatal error
1 | function fatal_handler() { |
PHP7开始,含十六进制字符串不再被认为是数字
1 | $str = "0xffff";//https://segmentfault.com/q/1010000004829059 |
php artisan config:cache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30cat config/sentry.php
return array(
'dsn' => env('SENTRY_LARAVEL_DSN'),
// capture release as git sha
// 'release' => trim(exec('git log --pretty="%h" -n1 HEAD')),
// Capture bindings on SQL queries
'breadcrumbs.sql_bindings' => true,
// Capture default user context
'user_context' => false,
//transport function
'transport'=>function(Raven_Client $raven_Client,&$data){
$raven_Client->setTransport(null);
$raven_Client->close_curl_resource();
\Queue::pushOn('sentry',new \App\Commands\sentry($raven_Client,$data));
},
);
'transport'=>new \app\SentryTransport(),
class SentryTransport
{
public static function __set_state($arr){
return function($raven_Client,&$data){
\Queue::pushOn('sentry',new \App\Commands\sentry($raven_Client,$data));
};
}
}
curl超时
1 | //重试 |
cURL error 28
1 | cURL error 28: See http://curl.haxx.se/libcurl/c/libcurl-errors.html |
格式化
1 | import prettytable as pt |
unicode
1 | Unicode 每个字符的详情,可以查官方文档: https://www.unicode.org/charts/ |
laravel更新后读取数据失败
1 | https://laravel-china.org/articles/16566/take-a-laravel-mysql-to-verify-the-experience-of-bug#reply62596 |
批量更新
1 | //https://laravel-china.org/topics/6864/how-to-batch-update-accumulate-a-field |
cat 多换行
1 | https://www.v2ex.com/t/188162 |
grep and or
1 | https://blog.csdn.net/jackaduma/article/details/6900242 |
memcached 默认的过期时间则 不能大于 30 天
https://laravel-china.org/topics/2461/laravel-session-using-memcached-stepping-on-the-pit
unix:///tmp/supervisor.sock refused connection
1 | [root@localhost supervisor]# supervisorctl -c /etc/supervisord.conf |
Specified key was too long
1 | https://laravel-china.org/articles/17094/larave-admin-installation?#reply64528 |
laravel with 查询中只查询个别字段
1 | select 的时候没有 id 的话就会为 null https://laravel-china.org/articles/17379 |
PHP输出A到Z
1 | for($i='A'; $i<='Z'; $i++) |
env函数的小坑
1 | dump(env('APP_ENV') === null); // true |
任务调度的列表
1 | //1. 加载Console内核 |
在模型事件中获取某字段修改前的值
1 | Issue::saving(function(Issue $issue){ |
laravel SerializesModels
因为我们在任务类里引用了 SerializesModels 这个 trait,使得 Eloquent 模型在处理任务时可以被优雅地序列化和反序列化。如果你的队列任务类在构造器中接收了一个 Eloquent 模型,那么只有可识别出该模型的属性会被序列化到队列里。当任务被实际运行时,队列系统便会自动从数据库中重新取回完整的模型。这整个过程对你的应用程序来说是完全透明的,这样可以避免在序列化完整的 Eloquent 模式实例时所带来的一些问题。https://laravel-china.org/topics/2993/laravel-novice-stepped-on-the-pit-to-share
composer require失败
1 | cat composer.json |
Fatal error: Class ‘PHPUnit_Framework_TestCase’ not found
1 | https://github.com/dees040/laravel-api-responses/issues/2 |
Laravel 的 collection
1 | $collection = new \Illuminate\Database\Eloquent\Collection(['item a', 'item b', 'item c']); |
使用”mews/captcha:~2.0” 验证码图片不显示问题
1 | Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined function Intervention\Image\Gd\imagettfbbox() in file /usr/share/nginx/html/LaraBBS/vendor/intervention/image/src/Intervention/Image/Gd/Font.php on line 85 |
在解析外部变量时的一个问题
1 | //https://segmentfault.com/a/1190000017051950 |
Laravel with () limit
1 | 比如如何取得每篇文章中的前10条评论 |
laravel 5.5 索引长度超过限制
1 | laravel5.4中改变了默认的编码类型(utf8 => utf8mb4), 从而使email字段索引长度超过了的默认限制长度 https://laravel-china.org/articles/18306 |
cron不执行
1 | 坑1:环境变量https://laravel-china.org/articles/18697 |
composer update后输出Killed
1 | //https://laravel-china.org/articles/20443 |
Memcached::get(): could not unserialize value, no igbinary support
1 | ./configure --enable-memcached --enable-memcached-igbinary --with-php-config= |
nginx 配置问题-FastCGI sent in stderr: “Primary script unknown”
1 | 一个关于 nginx 配置的问题,配置一个 php 网站,页面总是输出 File not found.,查看 nginx log 中报错: |
intervention/iamge下载图片过慢
1 | // 记录开始时间 |
MySQL由一个双引号引发的血案
1 | //https://mp.weixin.qq.com/s/TJsBPK-RbmvIS0PNJ8GqtQ |
foreach
1 | $arr = [1, 2, 3, 4]; |
proc_open()打开两个进程
1 | //https://blog.jiaojie.site/_posts/2017/08/15/proc_open-and-bash/ |
MySQL大数据分页
1 | https://blog.jiaojie.site/_posts/2017/12/22/mysql-pagination-tips/ |
每三位加个逗号分割
1 |
|
use & Referance
1 | use中传递php基本数据类型时,绝大多数情况都是传递的copy。 |
判断 Eloqument 模型查询数据结果是否为空
1 | return DB::table('orders')->where('finalized', 1)->exists(); |
requires ext-pcntl
1 | laravel/horizon v1.4.3 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system |
exec函数被禁
1 | 为了保证数据的有序性,需要对同时消费的进程进行数量限制(同时消费队列的进程不得超过1个) |
curl 用完千万别忘 close
1 | //https://laravel-china.org/articles/9172/curl-do-not-forget-close |
Memcached::get(): could not unserialize value, no igbinary support
1 | https://stackoverflow.com/questions/35200566/warning-memcachedgetmulti-could-not-unserialize-value-no-igbinary-support |
PHP 读取 ZIP乱码
1 | //https://laravel-china.org/topics/21866 |
Memcached::get()could not unserialize value, no igbinary support
1 | 使用laravel Cache::get()时报错,版本PHP7 ,报错文件 |
laravel redis 队列重复执行
1 | 现象 https://suren1986.in/index.php/archives/44/ |
MySQL导入SQL出现MySQL server has gone away错误
1 | MySQL导入SQL出现MySQL server has gone away错误 |
pdo查询出的数据均自动转换为字符串
1 | PDO::ATTR_STRINGIFY_FETCHES 提取的时候将数值转换为字符串。 |
git clone克隆速度过慢问题
1 | //https://blog.cooooler.com/2018/07/18/%E8%A7%A3%E5%86%B3git-clone%E5%85%8B%E9%9A%86%E9%80%9F%E5%BA%A6%E8%BF%87%E6%85%A2%E9%97%AE%E9%A2%98/ |
laravel中导出excel乱码
1 | //https://blog.cooooler.com/2018/05/11/PHP%E5%AF%BC%E5%87%BAexcel%E4%B9%B1%E7%A0%81%E7%9A%84%E7%BB%88%E6%9E%81%E8%A7%A3%E5%86%B3%E5%A4%A7%E6%B3%95/ |
laravel中进行PDF导出遇到的问题
1 | https://blog.cooooler.com/2018/08/29/%E6%80%BB%E7%BB%93%E4%B8%80%E4%B8%8Blaravel%E4%B8%AD%E8%BF%9B%E8%A1%8CPDF%E5%AF%BC%E5%87%BA%E9%81%87%E5%88%B0%E7%9A%84%E9%97%AE%E9%A2%98/ |
curl获取数据乱码
1 | composer require phlib/guzzle-middleware |
mysql emoji
1 | utf8_unicode_ci 只能存储 3 个字节的字符,无法应对 Emoji 这种需要 4 个字节的字符。https://suren1986.in/index.php/archives/45/ |
composer install、update出错需要Token
1 | 报错信息:使用SSH Key做身份认证克隆失败,请输入你的Github凭证来访问私人的镜像。(翻译得不好,大概是这个意思) |
MISCONF Redis is configured to save RDB snapshots
1 | https://laravel-china.org/articles/22025#fa405f |
laravel的坑
1 | https://www.jianshu.com/p/da9c0cae59ef |
crontab
1 | 新创建的cron job,不会马上执行,至少要过2分钟才执行。如果重启cron则马上执行。 |
laravel 读操连接了读库和写库
1 | /**vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php |
502 Bad Gateway
1 | [error] 26653#0: *748 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 10.235.51.48, server: xxx.cn, request: "GET /api/openapi.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9001", host: "xx.cn" |
ffmpeg: command not found
1 | 通过PHP调用这个命令,没有引入对应的环境变量,导致找不到这个命令 |
Warning: simplexml_load_file()
1 | Warning: simplexml_load_file(): I/O warning : failed to load external entity "xxx " |
同时读写一个文件的问题
1 | $fp = fopen("/tmp/lock.txt","w+"); |
nginx: [error] open() “/run/nginx.pid” failed
1 | [root@VM_0_14_centos gogs]# ps aux|grep nginx |
502
1 | Nginx的超时配置 |
php -a 无法使用
1 | //https://github.com/codcodog/Blog/issues/77 |
json_decode()返回null
1 | json_decode()函数,要求输入的字符串是有要求的: |
env忽略
1 | 在env中为DB_PASSWORD=abcde#142!*,但是在PHP代码中读取的数据库密码配置为abcde |
exceeded the timeout of 60 seconds
1 | [Symfony\Component\Process\Exception\ProcessTimedOutException] |
set_exception_handler() 不再保证收到的一定是 Exception 对象
1 | <?php |
二维码生成乱码
1 | composer require endroid/qr-code |
yum Error: rpmdb open failed
1 | yum install net-tools |
泄露 env 配置
1 | Laravel 默认依赖的 filp/whoops包 ,当 .env 文件中配置 APP_DEBUG=true 时,会在出错页面打印 .env 配置信息。 |
laravel 路由冲突
1 | //restful 查看 |
No ‘Access-Control-Allow-Origin’ header is present on the requested resource
1 | 发送非简单请求时,伺服器端会先收到一个 OPTIONS 的预请求,前端只有收到这个预请求的正常回应,才会发送正式的 POST 请求。 |
Git status中文乱码问题
1 | https://blog.huzhifeng.com/2016/03/08/Git-Tips-And-Tricks/ |
Cannot run program “svn”
1 | svn1.8使用了命令工具(command line client tools)默认安装SVN时是未安装此客户端的 |
svn比较版本 svn try setting http-chunked-requests to auto or no
1 | https://stackoverflow.com/questions/17399664/subversion-1-8-0-client-working-against-a-1-6-11-server |
ipv6惹的祸
1 | Operation timed out after 0 milliseconds with 0 out of 0 bytes received |
php 只接受1000个 请求参数
1 | 在查看php的error log 发现有句提示如下 |
502 Bad Gateway错误
1 | php程序执行时间过长而超时,检查nginx和fastcgi中各种timeout设置。 |
字符集转转换失败
1 | $xml = iconv('UTF-8', 'GBK', $xml); |
PHP7 MongoDB
1 | $mongo = new MongoDB\Driver\Manager(); |
MISCONF Redis is configured to save RDB snapshots
1 | Redis 被配置为保存 RDB 快照,但是现在无法写入磁盘,那么这无非是两个问题,一个是没有权限,一个是空间不足,于是立即登录到 Redis 所在服务器,分别查看了权限和空间都没有问题,当务之急是先修复问题,然后再定位原因,所以立即在服务启动 redis-cli,将 stop-writes-on-bgsave-error 设置为 on(该选项在 Redis 配置文件中默认配置为 yes,表示 Redis 报错时停止写入并报错,我们将其设置为 on 表示忽略报错,继续执行): |
mongodb3.6 The ‘cursor’ option is required, except for aggregate with the explain argument
1 | //Mongo-记一次安装启动异常 http://zhangyuyu.github.io/2017/12/27/Mongo-%E8%AE%B0%E4%B8%80%E6%AC%A1%E5%AE%89%E8%A3%85%E5%90%AF%E5%8A%A8%E9%94%99%E8%AF%AF/ |
php发送email 多了!
1 | https://blog.csdn.net/yylad/article/details/9048871 |
PhpSpreadsheet 输出 Excel 时自动在 xls 后面加入 html
1 | //http://www.sunbelife.com/p/b63c.html |
Composer 使用镜像出现权限问题
1 | vagrant@homestead:~/Code/larabbs$ composer require "overtrue/laravel-lang:~3.0" |
No query results for model
1 | 如果将 |
composer Out of memory
1 | $ composer require chenhua/laravel5-markdown-editor |
mysql 慢查询 502
1 | 大量请求响应为502,但每次故障发生时,错误响应一般集中在一台Web服务器 |
api频率限制
1 | // 每个IP一分钟10次 |
curl请求https
1 | $ curl 'https://x.x.x.x/get_ips' |
pip error: command ‘gcc’ failed with exit status
1 | Command /usr/bin/python -c "import setuptools;__file__='/tmp/pip-build-root/lxml/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-PjviBq-record/install-record.txt --single-version-externally-managed failed with error code 1 in /tmp/pip-build-root/lxml |