Skip to content

Instantly share code, notes, and snippets.

View forecho's full-sized avatar
🎯
Focusing

蔡正海 forecho

🎯
Focusing
View GitHub Profile
@forecho
forecho / httpClient.php
Last active November 18, 2015 10:11
封装 curl 类
<?php
function httpClient($url, $data = FALSE, $header = FALSE, $timeout = 30)
{
if (!strstr($url, 'http://') && !strstr($url, 'https://')) {
$url = 'http://' . $url;
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
@forecho
forecho / array_last.php
Created November 8, 2015 08:24
php获取数组最后一个数组值的三个方法
<?php
$array=array(1,2,3,4,5);
echo $array[count($array)-1];//计算数组长度,然后获取数组最后一个元素,如果数组中最后一个元素含有非数字键名,结果可能跟预期不符合
//适用于键名为数字的数组
echo '<br>';
echo end($array);//将数组的内部指针指向最后一个单元,适用于所有数组
echo '<br>';
rsort($array);//对数组逆向排序,如果数组中含有字母或汉字,结果可能不符合预期,最适用于数字数组
echo $array[0];
@forecho
forecho / random.php
Created November 8, 2015 05:28
百万数据库某个表「伪」随机显示 N 条数据思路
/**
* 随机
* @param int $rows 数据池
* @return mixed
*/
protected function random($rows = 100)
{
$count = User::find()->select(['itemid'])->where([])->count();
$random = mt_rand($rows, $count - $rows);
$model = User::find()->where(['>=', 'id', $random])->joinWith(['type'])->limit($rows)->all();
@forecho
forecho / controller.php
Last active July 15, 2022 11:20
Yii2 使用 CoreSeek 搜索 Demo 更多信息参考官方文档 http://www.coreseek.cn/products-install/step_by_step/
public function actionSearch()
{
$keyword = Yii::$app->request->get('keyword');
if (empty($keyword)) $this->goHome();
require_once(Yii::getAlias('@common/helpers') . '/sphinxapi.php');
$cl = new \SphinxClient ();
$cl->SetServer('127.0.0.1', 9312);
//以下设置用于返回数组形式的结果
$cl->SetArrayResult(true);
@forecho
forecho / uploadcontroller.php
Last active September 15, 2015 06:00 — forked from appcodr/uploadcontroller.php
REST API controller code in Yii2 to upload photo/file
<?php
class UploadController extends ActiveController
{
public $documentPath = 'documents/';
public function verbs()
{
$verbs = parent::verbs();
$verbs[ "upload" ] = ['POST' ];
@forecho
forecho / log.php
Last active September 14, 2015 02:05
Yii2 在线查看日志功能 使用方法:放在web目录下。
<?php
/**
* author : forecho <caizhenghai@gmail.com>
* createTime : 2015/7/9 16:32
* description:
*/
if (!isset($_GET['auth']) || $_GET['auth'] != "forecho") {
die();
}
@forecho
forecho / unlink.php
Last active August 29, 2015 14:26
删除匹配的所有文件
@forecho
forecho / Model.php
Last active July 15, 2022 11:23
Yii 2 表单验证JSON 二维数据
/**k
* @inheritdoc
*/
public function rules()
{
return [
[['name', 'subtitle'], 'string', 'max' => 100],
['productInfo', 'vaildateProductInfo'], // 自定义验证
['skus', 'vaildateSkus'], // 自定义验证
];
@forecho
forecho / Security.php
Last active August 29, 2015 14:22
创建一个随机字符串
<?php
/**
* author : forecho <caizhenghai@gmail.com>
* createTime : 2015/6/10 10:27
* description:
*/
class Security
{
/**
@forecho
forecho / 0_reuse_code.js
Last active August 29, 2015 14:21
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console