标签归档:php

Laravel 怎样实现 隐式路由

自动匹配 [模块 /控制器 /方法]:

Route::any('/{module}/{class}/{action}', function($module, $class, $action) {
    $ctrl = \App::make("\\App\\Http\\Controllers\\{$module}\\{$class}Controller");
    return \App::call([$ctrl, $action]);
});

PHP 利用 microtime() 获取毫秒时间戳

php的毫秒是没有默认函数的,但提供了一个microtime()函数,该函数返回包含两个元素,一个是秒数,一个是小数表示的毫秒数,借助此函数,可以很容易定义一个返回毫秒数的函数,例如:

function getMillisecond()
{
    list($s1, $s2) = explode(' ', microtime());
    return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
}

继续阅读

php 后期静态绑定

class MyClass {

  private $data;

  private function __construct($data) {
    $this->data = $data;
  }

  public static function run($data) {
    return (new static($data))->callfun();
  }

  private function callfun() {
    return $this->lookup($this->data);
  }

  private function lookup($data) {
    return $data;
  }
}

composer 安装 laravel 和 lumen

laravel 6.0

  • PHP >= 7.2.0
  • BCMath PHP 拓展
  • Ctype PHP 拓展
  • JSON PHP 拓展
  • Mbstring PHP 拓展
  • OpenSSL PHP 拓展
  • PDO PHP 拓展
  • Tokenizer PHP 拓展
  • XML PHP 拓展
composer global require "laravel/installer"
 // 创建新项目,blog是项目名(文件夹名)
composer create-project --prefer-dist laravel/laravel blog "6.0.*"

lumen

composer global require "laravel/lumen-installer"
lumen new blog

composer create-project --prefer-dist laravel/lumen blog

继续阅读

ubuntu 中利用 docker 搭建 php + oracle 开发环境

利用 docker 部署 oracle 11g

安装 docker

拉取镜像

docker pull sath89/oracle-xe-11g

创建实例

docker run -d --restart always -p 8080:8080 -p 1521:1521 -v /etc/localtime:/etc/localtime:ro -v /my/oracle/data:/u01/app/oracle sath89/oracle-xe-11g

<!–more–>

时间同步的参数详见: <http://jerry.red/453/docker-容器与宿主机时间不同步的解决办法>

连接信息

Connect database with following setting:

hostname: localhost
port: 1521
sid: xe
username: system
password: oracle
Password for SYS & SYSTEM:
oracle

Connect to Oracle Application Express web management console with following settings:

http://localhost:8080/apex
workspace: INTERNAL
user: ADMIN
password: oracle

镜像详情:<https://hub.docker.com/r/sath89/oracle-xe-11g/>,利用 daocloud.io 可加速拉取镜像。


安装 Oracle Instant Client

Install RPMs

  1. Download the Oracle Instantclient RPM files from http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html. Everyone needs either "Basic" or "Basic lite", and most users will want "SQL*Plus" and the "SDK".

  2. Convert these .rpm files into .deb packages and install using "alien" ("sudo apt-get install alien" if you don’t have it).

  3. For example, for version 12.1.0.2.0-1 for Linux x86_64 (64-bit):

alien -i oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm
alien -i oracle-instantclient12.1-sqlplus-12.1.0.2.0-1.x86_64.rpm
alien -i oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm
  1. Test your Instantclient install by using "sqlplus" or "sqlplus64" to connect to your database:
sqlplus system/oracle@//localhost:1521/xe

If you execute sqlplus and get "sqlplus: command not found", see the section below about adding the ORACLE_HOME variable.

If sqlplus complains of a missing libsqlplus.so file, follow the steps in the section "Integrate Oracle Libraries" below.

If sqlplus complains of a missing libaio.so.1 file, run

sudo apt-get install libaio1

or, if you’re installing the 32 bit instant client on 64 bit,

sudo apt-get install libaio1:i386

Integrate Oracle Libraries

If oracle applications, such as sqlplus, are complaining about missing libraries, you can add it to the system library list create a new file as follows:

sudo vi /etc/ld.so.conf.d/oracle.conf && sudo chmod o+r /etc/ld.so.conf.d/oracle.conf

and add the oracle library path as the first line. For example,

/usr/lib/oracle/12.1/client64/lib/

Then run ldconfig:

sudo ldconfig

ORACLE_HOME

Many Oracle database applications look for Oracle software in the location specified in the environment variable ‘ORACLE_HOME’.

Typical workstations will only have one Oracle install, and will want to define this variable in a system-wide location.

sudo vi /etc/profile.d/oracle.sh && sudo chmod o+r /etc/profile.d/oracle.sh

Add the following:

export ORACLE_HOME=/usr/lib/oracle/12.1/client64

Alternatively, each user can define this in their ~/.bash_profile

Note: From Ubuntu 11.04 (confirmed in 11.04 and 14.04) sqlplus was not recognized as a command unless the following line was also included in the oracle.sh file:

export PATH=$PATH:$ORACLE_HOME/bin

更多详情:<https://help.ubuntu.com/community/Oracle%20Instant%20Client>


安装 php 扩展

详见链接

PHP中插件机制的一种实现方案

插件,亦即Plug-in,是指一类特定的功能模块(通常由第三方开发者实现),它的特点是:当你需要它的时候激活它,不需要它的时候禁用/删除它;且无 论是激活还是禁用都不影响系统核心模块的运行,也就是说插件是一种非侵入式的模块化设计,实现了核心程序与插件程序的松散耦合。一个典型的例子就是 WordPress中众多的第三方插件,比如Akimet插件用于对用户的评论进行Spam过滤。

继续阅读

获取手机号码所属省份、城市、运营商Api接口

<?php
/**
 * 要查询的电话号码归属地
 * http://www.baidu.com/api.php?m=Tel&p=13281098058
 * {"province":"\u56db\u5ddd","city":"\u6210\u90fd","supplier":"\u8054\u901a"}
 * supplier 运营商 city 城市 province 省份
 * Some rights reserved:abc3210.com
 * Contact email:admin@abc3210.com
 */
class TelAction extends AppframeAction {
    public function index() {
        $mobile = $_GET['p'];  //要查询的电话号码
        $ko = $_GET['ko'];  //要查询的端口 1 为淘宝,2为财付通
        $data='';
        if($mobile){
           if($ko==1){
              $data=$this->taobao($mobile);
           }else{
              $data=$this->tenpay($mobile);
           }
        }
        exit($data);
    }

    private function taobao($mobile=0){//淘宝接口
        $url = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=".$mobile."&t=".time();  //根据淘宝的数据库调用返回值
        $content = file_get_contents($url);
        $data['province'] = substr($content, "56", "4");  //截取字符串
        $data['supplier'] = substr($content, "81", "4");
        return '{"province":"'.$data['province'].'","supplier":"'.$data['supplier'].'"}';
    }

    private function tenpay($mobile=0){//财付通接口
        $doc = new DOMDocument(); 
        $xmlurl='http://life.tenpay.com/cgi-bin/mobile/MobileQueryAttribution.cgi?chgmobile='.$mobile.'&f.xml';
        $doc->load($xmlurl); //读取xml文件 
        $xmls = $doc->getElementsByTagName("root"); //取得root标签的对象数组 
        foreach( $xmls as $xml ) { 
            $province = $xml->getElementsByTagName( "province" ); //省份
            $data['province'] = $this->delspace($province->item(0)->nodeValue); //省份
            $city = $xml->getElementsByTagName( "city" ); 
            $data['city']= $this->delspace($city->item(0)->nodeValue); //城市
            $supplier = $xml->getElementsByTagName( "supplier" ); 
            $data['supplier'] = $this->delspace($supplier->item(0)->nodeValue); //联通 移动 电信
        }
        return json_encode($data);
    }
    //过滤空格回车
    private function delspace($pcon){
         $pcon = preg_replace("/ /","",$pcon);
         $pcon = preg_replace("/ /","",$pcon);
         $pcon = preg_replace("/ /","",$pcon);
         $pcon = preg_replace("/\r\n/","",$pcon);
         $pcon = str_replace(chr(13),"",$pcon);
         $pcon = str_replace(chr(10),"",$pcon);
         $pcon = str_replace(chr(9),"",$pcon);
         return $pcon;
    }

}
?>