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、總之不會的找客服就行了。

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章