PHP短信发送服务 youe短信企业服务

     /**
     * 通用短信平台HTTP接口POST方式发送短信实例
     * 返回字符串
     * 一般情况下调用此方法
     */
    function postSendMessage($msgContents,$phoneListStr){ 
        //echo $phoneListStr.'<br/>';
        $ShortMSGUid='123456';//短信服务 用户名
        $ShortMSGPwd='123456';//短信服务 密码
        $ShortMSGSignature='【签名示例】';//短信服务 签名
        //$CustomerServicePhoneList=array('185********','185********');
        //$phoneListStr = implode(';',$CustomerServicePhoneList);
        
        $url="http://www.smsadmin.cn/smsmarketing/wwwroot/api/post_send/";   //通用短信平台接口地址
        $uid=$ShortMSGUid;         //您在通用短信平台上注册的用户ID
        //$uid=mb_convert_encoding($uid,'GB2312','UTF-8'); //内容为UTF-8时转码成GB2312
        $ShortMSGSignature = mb_convert_encoding($ShortMSGSignature,'GB2312','UTF-8');//有汉字的时候要加上
        $msgContents       = mb_convert_encoding($msgContents,'GB2312','UTF-8');
        $pwd=$ShortMSGPwd;         //用户密码 
        $msg=$msgContents.$ShortMSGSignature;//【通用短信平台】";         //要发送的短信内容,必须要加签名,签名格式:【签名内容】
        //$msg=mb_convert_encoding($msg,'GB2312','UTF-8'); //内容为UTF-8时转码成GB2312
        $mobile=$phoneListStr;    //接收短信的手机号码,多个手机号码用英文下的分号(;)间隔,最多不能超过1000个手机号码。
        $params = array(
            "uid"=>$uid,
            "pwd"=>$pwd,
            "mobile"=>$mobile,
            "msg"=>$msg,
            "dtime"=>"",   //为空,表示立即发送短信;写入时间即为定时发送短信时间,时间格式:0000-00-00 00:00:00
            "linkid"=>""   //为空,表示没有流水号;写入流水号,获取状态报告和短信回复时返回流水号,流水号格式要求:最大长度不能超过32位,数字、字母、数字字母组合的字符串
        );
        $results = $this->posttohosts($url,$params);
        /* 提交成功返回值格式:
        0发送成功! */
    }
    
    /**
     * 通过POST方式提交
     */
    function posttohosts($url, $data){
        $url = parse_url($url);
        if (!$url) return "couldn't parse url";
        if (!isset($url['port'])) { $url['port'] = ""; }
        if (!isset($url['query'])) { $url['query'] = ""; }
        $encoded = "";
        while (list($k,$v) = each($data)){
            $encoded .= ($encoded ? "&" : "");
            $encoded .= rawurlencode($k)."=".rawurlencode($v);
        }
        //echo $encoded;
        $fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80);
        if (!$fp) return "Failed to open socket to $url[host]";
        fputs($fp, sprintf("POST %s%s%s HTTP/1.0\n", $url['path'], $url['query'] ? "?" : "", $url['query']));
        fputs($fp, "Host: $url[host]\n");
        fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
        fputs($fp, "Content-length: " . strlen($encoded) . "\n");
        fputs($fp, "Connection: close\n\n");
        fputs($fp, "$encoded\n");
        $line = fgets($fp,1024);
        if (!preg_match("{^HTTP/1\.. 200}", $line)) return;
        $results = "";
        $inheader = 1;
        while(!feof($fp)){
            $line = fgets($fp,1024);
            if ($inheader && ($line == "\n" || $line == "\r\n")){
                $inheader = 0;
            }elseif (!$inheader){
                $results .= $line;
            }
        }
        fclose($fp);
        return $results;
    }

 

posted on 2016-11-29 22:54  CodeArtist  阅读(339)  评论(0编辑  收藏  举报