찾다
백엔드 개발PHP 튜토리얼PHP Hooks 및 간단한 배포 방법 분석 예시
PHP Hooks 및 간단한 배포 방법 분석 예시Apr 26, 2018 pm 03:25 PM
php사례분석방법

이 글에서는 주로 PHP Hooks와 간단한 배포 방법을 소개하며, PHP Hooks의 정의와 사용법을 예시로 분석하고, 여러 채널 간 자유로운 전환 구현 기술도 함께 설명합니다.

이 글은 다음과 같습니다. 기사에서는 간단한 배포 방법을 사용한 예제와 함께 PHP 후크를 설명합니다. 참고를 위해 모든 사람과 공유합니다. 세부 사항은 다음과 같습니다.

//简单的钩子实现例子
class tool{
  public static function main($class, $fun, $data = ''){
    //前置公共操作
    $con = new $class;
    $con->$fun($data);
    //后置公共操作
  }
}
class a{
  function b($data){
    echo '我是方法b';
  }
}
class c{
  function d($data){
    echo '我是方法d';
  }
}
//钩子调用
tool::main('a','b','222');

SMS 채널을 캡슐화할 때 후크를 사용하여 구현할 계획이었습니다.

자동으로 SMS(다중 채널) 이메일을 보내고 푸시할 수 있습니다. 그리고 다른 메시지. . .

나중에 비즈니스 요구 사항이 생각만큼 복잡하지 않다는 사실을 발견하고 개발이 보류되었습니다. . . .

T_T 그래서 우리는

class Ar_Sms{
  const LANCHUANG = 1;//通道1
  const ALIDAYU = 2; //通道2
  private $type;
  private $chuanglan_config = array(//通道1配置项
    'api_send_url'=>'xxxx',
    'api_balance_query_url'=> 'xxxxx',
    'api_account'=> 'xxxx',
    'api_password'=> 'xxxxx',
  );
  private $alidayu_config = array(//通道2配置项
    'api_key'=> 'xxxx',
    'api_id'=> 'xxxxx',
    'api_send_url'=> 'xxxxx',
  );
  public function __construct($type=1){
    switch($type){
      case self::LANCHUANG:
        $this->type = $type;break;
      case self::ALIDAYU:
        $this->type = $type;break;
      default:
        $this->type = false;
    }
  }
  //对外抛出的发送方法
  public function sendSms($mobile, $msg){
    switch($this->type){
      case self::LANCHUANG:
        return $this->_sendCL($mobile, $msg);
      case self::ALIDAYU:
        return $this->_sendAL($mobile, $msg);
      default:
        return false;
    }
  }
  //通道1发送方法
  private function _sendCL($mobile, $msg, $needstatus = 'false', $extno = ''){
    $postArr = array (
      'account' => $this->chuanglan_config['api_account'],
      'pswd' => $this->chuanglan_config['api_password'],
      'msg' => $msg,
      'mobile' => $mobile,
      'needstatus' => $needstatus,
      'extno' => $extno
    );
    $result = $this->_curlPost( $this->chuanglan_config['api_send_url'] , $postArr);
    $result = $this->_execResult($result);
    return $result[1] == 0 ? true : $result[1];
  }
  //通道2发送方法
  private function _sendAL($mobile, $msg){
    $postArr = array (
      'id' => $this->alidayu_config['api_id'],
      'key' => $this->alidayu_config['api_key'],
      'msg' => $msg,
      'mobile' => $mobile,
    );
    $result = $this->_curlPost( $this->alidayu_config['api_send_url'] , $postArr);
    $result = $this->_execResult($result);
    return $result[1] == 0 ? true : $result[1];
  }
  //-------------一些公共方法
  /**
   * 处理返回值\r\n 分割
   *
   */
  private function _execResult($result){
    $result=preg_split("/[,\r\n]/",$result);
    return $result;
  }
  /**
   * 处理返回值json
   *
   */
  private function _jsonResult($result){
    $result=json_decode($result, true);
    return $result;
  }
  /**
   * 通过CURL发送HTTP请求
   * @param string $url //请求URL
   * @param array $postFields //请求参数
   * @return mixed
   */
  private function _curlPost($url,$postFields){
    $postFields = http_build_query($postFields);
    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_POST, 1 );
    curl_setopt ( $ch, CURLOPT_HEADER, 0 );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postFields );
    $result = curl_exec ( $ch );
    curl_close ( $ch );
    return $result;
  }
}
$ob = new Ar_Sms(Ar_Sms::ALIDAYU);//通道1发送
$res = $ob->sendSms('xxxxx','xxxxxx');
var_dump($res);
$ob = new Ar_Sms(Ar_Sms::LANCHUANG);//通道2发送
$res = $ob->sendSms('xxxxx','xxxxxx');
var_dump($res);

간단한 배포를 통해 다중 채널의 자유로운 전환을 달성하기 위해 간단한 배포 방식을 채택했으며,

단순한 전송이므로 더 이상의 추상화는 없습니다~ 囧orz

후크의 구현 방법은 다소 야심적이므로 구체적인 구현을 여전히 고려해야 합니다. . 시간이 나면 공부하겠습니다. 여기에 간단한 데모가 있습니다. "다중 채널, 다중 방법, 다중 채널 지원, 확장 용이


위 내용은 PHP Hooks 및 간단한 배포 방법 분석 예시의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

See all articles

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

VSCode Windows 64비트 다운로드

VSCode Windows 64비트 다운로드

Microsoft에서 출시한 강력한 무료 IDE 편집기

SublimeText3 Linux 새 버전

SublimeText3 Linux 새 버전

SublimeText3 Linux 최신 버전

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

에디트플러스 중국어 크랙 버전

에디트플러스 중국어 크랙 버전

작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경