最近有朋友又让我帮它弄一下方维短信接口,程序是方维p2p的,好了,不多说了,我先说一下,是如何修改的,系统版本:3.5,,短信接口我依然是用56短信网的,其实这个如果自己会修改的话,其它的版本,肯定也是可以的
1.首先要开启短信的功能,如图所示::
2.把sms_trans.php文件上传至system/sms/dxw这个文件下(dxw这个文件夹需新建), dxw_sms.php这个文件上传至system/sms文件下,确认你的服务器或空间支持curl或者fsocket,建议把sms目录及目录里的文件读写权限修改为777
<?php // +---------------------------------------------------------------------- // | Fanweo2o商业系统 最新版V3.03.3285 含4个手机APP。 // +---------------------------------------------------------------------- // | Copyright (c) 2010 http://www.fanwe.com All rights reserved. // +---------------------------------------------------------------------- /* 模块的基本信息 */ if (isset($read_modules) && $read_modules == true) { $module['class_name'] = 'dxw'; /* 名称 */ //56短信网:http://www.56dxw.com $module['name'] = "56短信网 (<a href='http://www.56dxw.com' target='_blank'><font color='red'>还没账号?点击这免费注册</font></a>)"; $module['server_url'] = 'http://jiekou.56dxw.com/sms/HttpInterfaceMd5.aspx'; if(ACTION_NAME == "install" || ACTION_NAME == "edit"){ $module['lang'] = array(); $module['config'] = array(); } return $module; } // 企信通短信平台 require_once APP_ROOT_PATH."system/libs/sms.php"; //引入接口 require_once APP_ROOT_PATH."system/sms/dxw/sms_trans.php"; class dxw_sms implements sms { public $sms; public $message = ""; private $statusStr = array( "1" => "短信发送成功", "-2" => "除时间外,所有参数不能为空", "-3" => "用户名密码不正确", "-4" => "平台不存在", "-5" => "客户短信数量为0", "-6" => "客户账户余额小于要发送的条数", "-7" => "不能超过70个字", "-8" => "非法短信内容", "-9" => "未知系统故障", "-10" => "网络性错误", "-21" => "代表要加签名" ); public function __construct($smsInfo = '') { if(!empty($smsInfo)) { $this->sms = $smsInfo; } } public function sendSMS($mobile_number,$content,$is_adv=0) { if(is_array($mobile_number)) { $mobile_number = implode(",",$mobile_number); } $sms = new sms_trans(); $params = array( "username"=>$this->sms['user_name'], "userpwd"=>md5($this->sms['password']), "handtel"=>$mobile_number, "sendcontent"=>urlencode(iconv("UTF-8","gbk",$content)), "sendtime"=>'', "smsnumber"=>10690, "comid"=>2714 ); $result = $sms->request($this->sms['server_url'],$params); $code = $result['body']; if($code=='0') { $result['status'] = 1; } else { $result['status'] = 0; $result['msg'] = $this->statusStr[$code]; } return $result; } public function getSmsInfo() { return "56短信网(最好的短信平台)"; } public function check_fee() { global $username,$userpwd; es_session::start(); $last_visit = intval(es_session::get("last_visit_dxw")); if(get_gmtime() - $last_visit > 10) { $sms = new sms_trans(); $username=$this->sms['user_name']; $userpwd=$this->sms['password']; $url = "http://jiekou.56dxw.com/sms/HttpInterfaceR.aspx?comid=2714&username=$username&userpwd=$userpwd&newpwd=&action=moneyc"; $string = file_get_contents($url); $match = explode('#',$string); if ($username!= '') { $str = sprintf($match[0]); } else { $str = sprintf('您刚获取过短信余额条数了,等等再获取吧'); } es_session::set("dxw_info",$str); es_session::set("last_visit_dxw",get_gmtime()); return $str; } else { $str = sprintf('请稍后再试'); } } } ?>sms_trans.php文件
<?php class sms_trans { /** * 脚本执行时间。-1表示采用PHP的默认值。 * * @access private * @var integer $time_limit */ var $time_limit = -1; /** * 在多少秒之内,如果连接不可用,脚本就停止连接。-1表示采用PHP的默认值。 * * @access private * @var integer $connect_timeout */ var $connect_timeout = -1; /** * 连接后,限定多少秒超时。-1表示采用PHP的默认值。此项仅当采用CURL库时启用。 * * @access private * @var integer $stream_timeout */ var $stream_timeout = -1; /** * 是否使用CURL库来连接。false表示采用fsockopen进行连接。 * * @access private * @var boolean $use_curl */ var $use_curl = false; /** * 构造函数 * * @access public * @param integer $time_limit * @param integer $connect_timeout * @param integer $stream_timeout * @param boolean $use_curl * @return void */ function __construct($time_limit = -1, $connect_timeout = -1, $stream_timeout = -1, $use_curl = false) { //$this->transport($time_limit, $connect_timeout, $stream_timeout, $use_curl); $this->time_limit = $time_limit; $this->connect_timeout = $connect_timeout; $this->stream_timeout = $stream_timeout; $this->use_curl = $use_curl; } /** * 请求远程服务器 * * @access public * @param string $url 远程服务器的URL * @param mix $params 查询参数,形如bar=foo&foo=bar;或者是一维关联数组,形如array('a'=>'aa',...) * @param string $method 请求方式,是POST还是GET * @param array $my_header 用户要发送的头部信息,为一维关联数组,形如array('a'=>'aa',...) * @return array 成功返回一维关联数组,形如array('header'=>'bar', 'body'=>'foo'), * 重大错误程序直接停止运行,否则返回false。 */ function request($url, $params = '', $method = 'POST', $my_header = '') { $fsock_exists = function_exists('fsockopen'); $curl_exists = function_exists('curl_init'); if (!$fsock_exists && !$curl_exists) { die('No method available!'); } if (!$url) { die('Invalid url!'); } if ($this->time_limit > -1)//如果为0,不限制执行时间 { set_time_limit($this->time_limit); } $method = $method === 'GET' ? $method : 'POST'; $response = ''; $temp_str = ''; /* 格式化将要发要送的参数 */ if ($params && is_array($params)) { foreach ($params AS $key => $value) { $temp_str .= '&' . $key . '=' . $value; } $params = preg_replace('/^&/', '', $temp_str); } /* 如果fsockopen存在,且用户不指定使用curl,则调用use_socket函数 */ if ($fsock_exists && !$this->use_curl) { $response = $this->use_socket($url, $params, $method, $my_header); } /* 只要上述条件中的任一个不成立,流程就转向这里,这时如果curl模块可用,就调用use_curl函数 */ elseif ($curl_exists) { $response = $this->use_curl($url, $params, $method, $my_header); } /* 空响应或者传输过程中发生错误,程序将返回false */ if (!$response) { return false; } return $response; } /** * 使用fsockopen进行连接 * * @access private * @param string $url 远程服务器的URL * @param string $params 查询参数,形如bar=foo&foo=bar * @param string $method 请求方式,是POST还是GET * @param array $my_header 用户要发送的头部信息,为一维关联数组,形如array('a'=>'aa',...) * @return array 成功返回一维关联数组,形如array('header'=>'bar', 'body'=>'foo'), * 否则返回false。 */ function use_socket($url, $params, $method, $my_header) { $query = ''; $auth = ''; $content_type = ''; $content_length = ''; $request_body = ''; $request = ''; $http_response = ''; $temp_str = ''; $error = ''; $errstr = ''; $crlf = $this->generate_crlf(); if ($method === 'GET') { $query = $params ? "?$params" : ''; } else { $request_body = $params; $content_type = 'Content-Type: application/x-www-form-urlencoded' . $crlf; $content_length = 'Content-Length: ' . strlen($request_body) . $crlf . $crlf; } $url_parts = $this->parse_raw_url($url); $path = $url_parts['path'] . $query; if (!empty($url_parts['user'])) { $auth = 'Authorization: Basic ' . base64_encode($url_parts['user'] . ':' . $url_parts['pass']) . $crlf; } /* 格式化自定义头部信息 */ if ($my_header && is_array($my_header)) { foreach ($my_header AS $key => $value) { $temp_str .= $key . ': ' . $value . $crlf; } $my_header = $temp_str; } /* 构造HTTP请求头部 */ $request = "$method $path HTTP/1.0$crlf" . 'Host: ' . $url_parts['host'] . $crlf . $auth . $my_header . $content_type . $content_length . $request_body; if ($this->connect_timeout > -1) { $fp = @fsockopen($url_parts['host'], $url_parts['port'], $error, $errstr, $connect_timeout); } else { $fp = @fsockopen($url_parts['host'], $url_parts['port'], $error, $errstr); } if (!$fp) { return false;//打开失败 } if (!@fwrite($fp, $request)) { return false;//写入失败 } while (!feof($fp)) { $http_response .= fgets($fp); } if (!$http_response) { return false;//空响应 } $separator = '/\r\n\r\n|\n\n|\r\r/'; list($http_header, $http_body) = preg_split($separator, $http_response, 2); $http_response = array('header' => $http_header,//header肯定有值 'body' => $http_body);//body可能为空 @fclose($fp); return $http_response; } /** * 使用curl进行连接 * * @access private * @param string $url 远程服务器的URL * @param string $params 查询参数,形如bar=foo&foo=bar * @param string $method 请求方式,是POST还是GET * @param array $my_header 用户要发送的头部信息,为一维关联数组,形如array('a'=>'aa',...) * @return array 成功返回一维关联数组,形如array('header'=>'bar', 'body'=>'foo'), * 失败返回false。 */ function use_curl($url, $params, $method, $my_header) { /* 开始一个新会话 */ $curl_session = curl_init(); /* 基本设置 */ curl_setopt($curl_session, CURLOPT_FORBID_REUSE, true); // 处理完后,关闭连接,释放资源 curl_setopt($curl_session, CURLOPT_HEADER, true);//结果中包含头部信息 curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);//把结果返回,而非直接输出 curl_setopt($curl_session, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);//采用1.0版的HTTP协议 $url_parts = $this->parse_raw_url($url); /* 设置验证策略 */ if (!empty($url_parts['user'])) { $auth = $url_parts['user'] . ':' . $url_parts['pass']; curl_setopt($curl_session, CURLOPT_USERPWD, $auth); curl_setopt($curl_session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); } $header = array(); /* 设置主机 */ $header[] = 'Host: ' . $url_parts['host']; /* 格式化自定义头部信息 */ if ($my_header && is_array($my_header)) { foreach ($my_header AS $key => $value) { $header[] = $key . ': ' . $value; } } if ($method === 'GET') { curl_setopt($curl_session, CURLOPT_HTTPGET, true); $url .= $params ? '?' . $params : ''; } else { curl_setopt($curl_session, CURLOPT_POST, true); $header[] = 'Content-Type: application/x-www-form-urlencoded'; $header[] = 'Content-Length: ' . strlen($params); curl_setopt($curl_session, CURLOPT_POSTFIELDS, $params); } /* 设置请求地址 */ curl_setopt($curl_session, CURLOPT_URL, $url); /* 设置头部信息 */ curl_setopt($curl_session, CURLOPT_HTTPHEADER, $header); if ($this->connect_timeout > -1) { curl_setopt($curl_session, CURLOPT_CONNECTTIMEOUT, $this->connect_timeout); } if ($this->stream_timeout > -1) { curl_setopt($curl_session, CURLOPT_TIMEOUT, $this->stream_timeout); } /* 发送请求 */ $http_response = curl_exec($curl_session); if (curl_errno($curl_session) != 0) { return false; } $separator = '/\r\n\r\n|\n\n|\r\r/'; list($http_header, $http_body) = preg_split($separator, $http_response, 2); $http_response = array('header' => $http_header,//肯定有值 'body' => $http_body); //可能为空 curl_close($curl_session); return $http_response; } /** * Similar to PHP's builtin parse_url() function, but makes sure what the schema, * path and port keys are set to http, /, 80 respectively if they're missing * * @access private * @param string $raw_url Raw URL to be split into an array * @author http://www.cpaint.net/ * @return array */ function parse_raw_url($raw_url) { $retval = array(); $raw_url = (string) $raw_url; // make sure parse_url() recognizes the URL correctly. if (strpos($raw_url, '://') === false) { $raw_url = 'http://' . $raw_url; } // split request into array $retval = parse_url($raw_url); // make sure a path key exists if (!isset($retval['path'])) { $retval['path'] = '/'; } // set port to 80 if none exists if (!isset($retval['port'])) { $retval['port'] = '80'; } return $retval; } /** * 产生一个换行符,不同的操作系统会有不同的换行符 * * @access private * @return string 用双引号引用的换行符 */ function generate_crlf() { $crlf = ''; if (strtoupper(substr(PHP_OS, 0, 3) === 'WIN')) { $crlf = "\r\n"; } elseif (strtoupper(substr(PHP_OS, 0, 3) === 'MAC')) { $crlf = "\r"; } else { $crlf = "\n"; } return $crlf; } } ?>3.查看短信列表,系统设置----短信邮件管理-------短信接口列表 ,如图,就可以看到56短信网接口网关了
1.首先删除这二个文件
2.点击短信接口列表后可以显示成功了,随边安装一个,自带的,点击使用该短信接口是否正常。如果显示是正在使用中,说明是没有问题的,如果什么也不显示,可能是安装程序有问题。
3.如果自带的可以,这时候你把修改过的这二个文件再上传到相应的目录下。
4.点击短信接口列表这个时候应该是可以正常显示的,你把自带的卸载了,再重新安装一下你这个,看一下是否可以。
其实 对于高手来讲,以上都不算什么,但对于php不是太精通的人来讲,接入的时候,还是可以参考的。
如果有不明白的可以加我微信:jjzaihaozhe
_--占