type=$type; if(!isset($type)||$type==1){ $this->encryptKey=config('ymsms.password'); $this->appid=config('ymsms.cdkey'); }else{ $this->encryptKey=config('ymsms2.password'); $this->appid=config('ymsms2.cdkey'); } } //加密 public function encrypt($encryptStr) { if(version_compare(PHP_VERSION,'7.0.0','ge')){ $encryptObj = new MagicCrypt($this->type); return $encryptObj->encryptPhp7($encryptStr);//加密结果 } $localIV = $this->iv; $encryptKey = $this->encryptKey; if (true == EN_GZIP) $encryptStr = gzencode($encryptStr); //Open module $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, $localIV); mcrypt_generic_init($module, $encryptKey, $localIV); //Padding $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); $pad = $block - (strlen($encryptStr) % $block); //Compute how many characters need to pad $encryptStr .= str_repeat(chr($pad), $pad); // After pad, the str length must be equal to block or its integer multiples //encrypt $encrypted = mcrypt_generic($module, $encryptStr); //Close mcrypt_generic_deinit($module); mcrypt_module_close($module); return $encrypted; } //PHP7使用这个加密 public function encryptPhp7($encryptStr){ $localIV = $this->iv; $encryptKey = $this->encryptKey; if (true == EN_GZIP) $encryptStr = gzencode($encryptStr); $ivlen = openssl_cipher_iv_length("AES-128-ECB"); $localIV=openssl_random_pseudo_bytes($ivlen); return openssl_encrypt($encryptStr, 'AES-128-ECB',$encryptKey,OPENSSL_RAW_DATA,$localIV); } //解密 public function decrypt($encryptStr) { if(version_compare(PHP_VERSION,'7.0.0','ge')){ $encryptObj = new MagicCrypt($this->type); return $encryptObj->decryptPhp7($encryptStr);//加密结果 } $localIV = $this->iv; $encryptKey = $this->encryptKey; //Open module $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, $localIV); mcrypt_generic_init($module, $encryptKey, $localIV); $encryptedData = mdecrypt_generic($module, $encryptStr); if (true == EN_GZIP) $encryptedData = gzdecode($encryptedData); return $encryptedData; } //PHP7使用这个解密 public function decryptPhp7($encryptStr){ $localIV = $this->iv; $encryptKey = $this->encryptKey; $ivlen = openssl_cipher_iv_length("AES-128-ECB"); $localIV=openssl_random_pseudo_bytes($ivlen); $encryptedData = openssl_decrypt($encryptStr,'AES-128-ECB',$encryptKey,OPENSSL_RAW_DATA,$localIV); if (true == EN_GZIP) $encryptedData = gzdecode($encryptedData); return $encryptedData; } } class Ymsms{ private $encryptKey = ""; private $appid=''; private $type=''; public function __construct($type=1){ $this->type=$type; if($type==1){ $this->encryptKey=config('ymsms.password'); $this->appid=config('ymsms.cdkey'); }else{ $this->encryptKey=config('ymsms2.password'); $this->appid=config('ymsms2.cdkey'); } } function http_request($url, $data = null){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, true); $header[] = "appId: ".$this->appid; if(true == EN_GZIP)$header[] = "gzip: on"; curl_setopt($curl, CURLOPT_HTTPHEADER, $header); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_HEADER, true); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); $res = curl_exec($curl); $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); curl_close($curl); $header = substr($res, 0, $headerSize); $outobj =array(); $lines = explode("\r\n",$header); foreach($lines as $line){ $items = explode(": ",$line); if(isset($items[0])&&!empty($items[0])&&isset($items[1])&&!empty($items[1])){ $key=$items[0]; $outobj[$key]=$items[1]; } } $outobj=(Object)$outobj; $outobj->ciphertext = substr($res, $headerSize); return $outobj; } function getMillisecond() { list($t1, $t2) = explode(' ', microtime()); return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000); } function SendSimpleSMS($mobile, $content, $timerTime = "", $customSmsId = "",$extendedCode = "", $validPeriodtime= 120){ $item = new stdClass(); $item->mobile = $mobile; $item->content = $content; /* 选填内容 */ if("" != $timerTime)$item->timerTime = $timerTime; if("" != $customSmsId)$item->customSmsId = $customSmsId; if("" != $extendedCode)$item->extendedCode = $extendedCode; $item->requestTime = $this->getMillisecond(); $item->requestValidPeriod = $validPeriodtime; $json_data = json_encode($item, JSON_UNESCAPED_UNICODE); $encryptObj = new MagicCrypt($this->type); $senddata = $encryptObj->encrypt($json_data);//加密结果 $url = YM_SMS_ADDR.YM_SMS_SEND_URI; $resobj = $this->http_request($url, $senddata); $resobj->plaintext = $resobj->ciphertext?$encryptObj->decrypt($resobj->ciphertext):''; return $resobj; } function getMo($number =0,$validPeriodtime= 120){ $item = new stdClass(); /* 选填内容 */ if(0 != $number) $item->number = $number; $item->requestTime = $this->getMillisecond(); $item->requestValidPeriod = $validPeriodtime; $json_data = json_encode($item, JSON_UNESCAPED_UNICODE); $encryptObj = new MagicCrypt($this->type); $senddata = $encryptObj->encrypt($json_data);//加密结果 $url = YM_SMS_ADDR.YM_SMS_GETMO_URI; $resobj = $this->http_request($url, $senddata); $resobj->plaintext =$resobj->ciphertext?$encryptObj->decrypt($resobj->ciphertext):''; return $resobj; } } ?>