|
- <?php
- /**
- *
- * ============================================================================
- * * 版权所有 蜘蛛出行 * *
- * 网站地址: http://www.zhizhuchuxing.com
- * ----------------------------------------------------------------------------
- * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
- * 使用;不允许对程序代码以任何形式任何目的的再发布。
- * ============================================================================
- * Author By: 温依莅
- * PhpStorm msg.php
- * Create By 2017/2/23 10:19 $
- */
-
- namespace common\models;
-
-
- class Msg
- {
-
- static protected $sinaAppKey = 3271760578;
- const MESSAGE_ACCOUNT = 'dh78301';
- const MESSAGE_PASSWORD = 'jGRk34B7';
-
- /**
- * Function Description:发送http请求
- * Function Name: httpRequest
- * @param string $url
- * @param array $data
- *
- * @return mixed
- *
- * @author Steven
- */
- static function httpRequest($url, $data = null)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- if (!empty($data)) {
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- }
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $output = curl_exec($ch);
- curl_close($ch);
- return $output;
- }
-
- /**
- * Des:发送异步请求
- * Name: httpRequestForasyn
- * @param $url
- * @param null $data
- * @author 倪宗锋
- */
- static function httpRequestForasyn($url, $data = null)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- if (!empty($data)) {
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- }
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_TIMEOUT, 1);
- curl_exec($ch);
- curl_close($ch);
- }
-
- static function httpRequest2($url, $post_data)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $json_data = curl_exec($ch);
- return $json_data;
- }
-
- static function send_post_json($url, $data_string)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json; charset=utf-8',
- 'Content-Length: ' . strlen($data_string))
- );
- ob_start();
- curl_exec($ch);
- $return_content = ob_get_contents();
- ob_end_clean();
- $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- return array($return_code, $return_content);
- }
-
- /**
- * 获取短信头
- * @param $main_corp_id
- * @return mixed
- */
- static function getSign($main_corp_id = 1)
- {
- $signArr = \Yii::$app->params['msg_web_name'];
- if (isset($signArr[$main_corp_id])) {
- return $signArr[$main_corp_id];
- } else {
- return $signArr[1];
- }
- }
-
- static function sendTelMsg($phones, $content, $main_corp_id = 1){
- $send_data = array();
- $send_data["account"] = self::MESSAGE_ACCOUNT;
- $send_data["password"] = strtolower(md5(self::MESSAGE_PASSWORD));
- $send_data["sign"] = static::getSign($main_corp_id);
- $send_data["phones"] = $phones;
- $send_data["content"] = $content;
- $url = 'http://www.dh3t.com/json/sms/Submit';
- $response = self::send_post_json($url, urldecode(json_encode($send_data)));
- $error_code = $response[0] == 200 ? 0 : -1;
-
- return $error_code;
- }
-
- /**
- * Function Description:获取短信
- * Function Name: actionGetTailorMessage
- * @param $main_corp_id int
- *
- * @return mixed|string
- *
- * @author LUOCJ
- */
- static function getMessage($main_corp_id = 1)
- {
- $send_data = array();
- $send_data["account"] = self::MESSAGE_ACCOUNT;
- $send_data["password"] = strtolower(md5(self::MESSAGE_PASSWORD));
- $send_data["sign"] = static::getSign($main_corp_id);
- $url = "http://www.dh3t.com/json/sms/Deliver";
- $response = Msg::send_post_json($url, urldecode(json_encode($send_data)));
- $error_code = $response[0] == 200 ? 0 : -1;
- if ($error_code == -1) {
- return false;
- } else {
- return $response;
- }
-
- }
-
-
- /**
- * Function Description:获取请求结果
- * Function Name: curlQuery
- * @param $url
- *
- * @return mixed
- *
- * @author Steven
- */
- static function curlQuery($url)
- {
- //设置附加HTTP头
- $addHead = array(
- "Content-type: application/json"
- );
- //初始化curl,当然,你也可以用fsockopen代替
- $curl_obj = curl_init();
- //设置网址
- curl_setopt($curl_obj, CURLOPT_URL, $url);
- //附加Head内容
- curl_setopt($curl_obj, CURLOPT_HTTPHEADER, $addHead);
- //是否输出返回头信息
- curl_setopt($curl_obj, CURLOPT_HEADER, 0);
- //将curl_exec的结果返回
- curl_setopt($curl_obj, CURLOPT_RETURNTRANSFER, 1);
- //设置超时时间
- curl_setopt($curl_obj, CURLOPT_TIMEOUT, 15);
- //执行
- $result = curl_exec($curl_obj);
- //关闭curl回话
- curl_close($curl_obj);
- return $result;
- }
-
- /**
- * Function Description:简单处理下url,sina对于没有协议(http://)开头的和不规范的地址会返回错误
- * Function Name: filterUrl
- * @param string $url
- *
- * @return bool|string
- *
- * @author Steven
- */
- static function filterUrl($url = '')
- {
- $url = trim(strtolower($url));
- $url = trim(preg_replace('/^http:\//', '', $url));
- if ($url == '')
- return false;
- else
- return urlencode('http://' . $url);
- }
-
- /**
- * Function Description:根据长网址获取短网址
- * Function Name: sinaShortenUrl
- * @param $long_url
- *
- * @return bool
- *
- * @author Steven
- */
- static function sinaShortenUrl($long_url)
- {
- return "";//test
- /*
- $access_url = "https://api-ssl.bitly.com/v3/shorten?access_token=".BITACCESSTOKEN."&longUrl=".$long_url;
- $result = httpRequest($access_url);
- $return_result = json_decode($result,true);
- if( $return_result["status_code"] == 200 ) {
- $url = $return_result["data"]["url"];
- $return_url = str_replace("http://", "https://", $url);
- return $return_url;
- }
- */
- //拼接请求地址,此地址你可以在官方的文档中查看到
- $url = 'http://api.t.sina.com.cn/short_url/shorten.json?source=' . self::$sinaAppKey . '&url_long=' . $long_url;
- //获取请求结果
- $result = self::curlQuery($url);
- //下面这行注释用于调试,
- //print_r($result);exit();
- //解析json
- $json = json_decode($result);
- //异常情况返回false
- if (isset($json->error) || !isset($json[0]->url_short) || $json[0]->url_short == '')
- return false;
- else
- return $json[0]->url_short;
- }
-
- /**
- * Function Description:根据短网址获取长网址,此函数重用了不少sinaShortenUrl中的代码,以方便阅读对比,可以自行合并两个函数
- * Function Name: sinaExpandUrl
- * @param $short_url
- *
- * @return bool
- *
- * @author Steven
- */
- static function sinaExpandUrl($short_url)
- {
- //拼接请求地址,此地址你可以在官方的文档中查看到
- $url = 'http://api.t.sina.com.cn/short_url/expand.json?source=' . self::$sinaAppKey . '&url_short=' . $short_url;
- //获取请求结果
- $result = self::curlQuery($url);
- //下面这行注释用于调试
- //print_r($result);exit();
- //解析json
- $json = json_decode($result);
- //异常情况返回false
- if (isset($json->error) || !isset($json[0]->url_long) || $json[0]->url_long == '')
- return false;
- else
- return $json[0]->url_long;
- }
- }
|