php接入飞鸽传书短信

一、后台获取所需参数

后台地址:http://sms.feige.ee/
首页获取两个需要的参数

  • 接口账号
  • 接口密钥
    在这里插入图片描述
    短信中心获取两个所需参数
  • 签名Id
  • 模板id
    在这里插入图片描述
    在这里插入图片描述

二、开放文档

开放文档地址http://www.febook.cn/dev/dev8
在这里插入图片描述

三、代码实现

public function send()
    {
        $phone = input('phone');
        if(empty($phone)){
            $json=array('status'=>400,'msg'=>'请输入手机号');
            exit(json_encode($json));
        }
        $code = mt_rand(1000, 9999);
        $data['Account'] = '接口账号';
        $data['Pwd'] = '接口密钥';
        $data['Content'] = $code;
        $data['Mobile'] = $phone;
        $data['TemplateId'] = 这儿是模板id;
        $data['SignId'] = 这儿是签名id;
        $url = "http://api.feige.ee/SmsService/Template";
        $res = $this->post($url, $data);
        $result = json_decode($res, true);
        if ($result['Code'] == 0 && $result['Message'] == 'OK') {
            Session::set($phone,$code,60);
            $json = array('status' => 200, 'msg' => '发送成功');
            exit(json_encode($json));
        } else {
            $json = array('status' => 1000, 'msg' => '发送失败');
            exit(json_encode($json));
        }
    }

    /**
     * 发送短信
     */
    function post($url, $data, $proxy = null, $timeout = 20)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); //在HTTP请求中包含一个"User-Agent: "头的字符串。
        curl_setopt($curl, CURLOPT_HEADER, 0); //启用时会将头文件的信息作为数据流输出。
        curl_setopt($curl, CURLOPT_POST, true); //发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);//Post提交的数据包
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); //启用时会将服务器服务器返回的"Location: "放在header中递归的返回给服务器,使用CURLOPT_MAXREDIRS可以限定递归返回的数量。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //文件流形式
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); //设置cURL允许执行的最长秒数。
        $content = curl_exec($curl);
        curl_close($curl);
        unset($curl);
        return $content;
    }

温馨提示:

1、有时候第一次发需要客服授权或者延迟,直接联系客服就行。
2、如果是国际短信,手机号前面需要加上86
3、总之不会的找客服就行了。

在这里插入图片描述

发布了328 篇原创文章 · 获赞 110 · 访问量 47万+

猜你喜欢

转载自blog.csdn.net/qq_42249896/article/details/102962918