服务器禁用allow_urlopen,如何用cURL替代file()实现短信API功能?
我明白你遇到的问题——服务器禁用了allow_url_fopen导致原本用file()函数调用短信API的代码跑不起来。别担心,我已经把代码全部改成用cURL来实现HTTP请求了,这是修改后的完整代码:
<?php //Check if sms notification is enabled if(isset($configInfo['sms_note']) AND $configInfo['sms_note'] == 'Enabled'){ $newPhone = str_replace('+', '', $phone); $SMSCode = $SMSCode; $SMSsender = $configInfo['sms_sender']; $SMSport = $configInfo['sms_port']; $SMSurl = $configInfo['sms_gateway_url']; $SMSusername = $configInfo['sms_gateway_user']; $SMSpassword = $configInfo['sms_gateway_pass']; $SMSsitename = $siteInfo['site_name']; class Sender { var $host; var $port; var $strUserName; var $strPassword; var $strSender; var $strMessage; var $strMobile; var $strMessageType; var $strDlr; var $phoneCode; var $newPhone; var $SMSCode; var $SMSsender; var $SMSport; var $SMSusername; var $SMSpassword; var $SMSsitename; var $senderName; private function sms__unicode($message) { $hex1 = ''; if (function_exists('iconv')) { $latin = @iconv('UTF-8', 'ISO-8859-1', $message); if (strcmp($latin, $message)) { $arr = unpack('H*hex', @iconv('UTF-8', 'UCS-2BE',$message)); $hex1 = strtoupper($arr['hex']); } if ($hex1 == '') { $hex2 = ''; $hex = ''; for ($i = 0; $i < strlen($message); $i++) { $hex = dechex(ord($message[$i])); $len = strlen($hex); $add = 4 - $len; if ($len < 4) { for ($j = 0; $j < $add; $j++) { $hex = "0" . $hex; } } $hex2.=$hex; } return $hex2; } else { return $hex1; } } else { print 'iconv Function Not Exists !'; } } //Constructor.. public function Sender($host, $port, $username, $password, $sender, $message, $mobile, $msgtype, $dlr) { global $phoneCode; global $newPhone; global $SMSCode; global $SMSsender; global $SMSport; global $SMSurl; global $SMSusername; global $SMSpassword; global $SMSsitename; $this->host = $SMSurl; $this->port = $SMSport; $this->strUserName = $SMSusername; $this->strPassword = $SMSpassword; $this->strSender = $SMSsender; $this->strMessage = $SMSsitename." Phone Verification Code: ".$SMSCode.". Thanks"; $this->strMobile = $newPhone; $this->strMessageType = 0; $this->strDlr = 0; } public function Submit() { $port = ""; if($this->port == '') { $port = ":" . $this->port; } if ($this->strMessageType == "2" || $this->strMessageType == "6") { //Call The Function Of String To HEX. $this->strMessage = $this->sms__unicode($this->strMessage); try { //Smpp http Url to send sms. $live_url = "http://" . $this->host . ":" . $this->port . "/bulksms/bulksms?username=" . $this->strUserName . "&password=" . $this->strPassword . "&type=" . $this->strMessageType . "&dlr=" . $this->strDlr . "&destination=" . $this->strMobile . "&source=" . $this->strSender . "&message=" . $this->strMessage . ""; // 使用cURL替代file() $ch = curl_init($live_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $parse_url = curl_exec($ch); curl_close($ch); echo $parse_url; } catch (Exception $e) { echo 'Message:' . $e->getMessage(); } } else { $this->strMessage = urlencode($this->strMessage); try { //Smpp http Url to send sms. $live_url = "http://" . $this->host . ":" . $this->port. "/bulksms/bulksms?username=" . $this->strUserName . "&password=" . $this->strPassword . "&type=" . $this->strMessageType . "&dlr=" . $this->strDlr . "&destination=" . $this->strMobile . "&source=" . $this->strSender . "&message=" . $this->strMessage . ""; // 使用cURL替代file() $ch = curl_init($live_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $parse_url = curl_exec($ch); curl_close($ch); echo $parse_url; } catch (Exception $e) { echo 'Message:' . $e->getMessage(); } } } } //Call The Constructor. $obj = new Sender("IP/DOMAIN","PORT","USERNAME","PASSWORD","SENDER/SOURCE","MESSAGE","DESTINATION","MESSAGE TYPE","1"); $obj->Submit (); } ?>
关键修改说明:
- 完全替换了原本
file()函数的调用逻辑,改用cURL来发起HTTP请求,这是allow_url_fopen禁用时的标准替代方案 - 设置了
CURLOPT_RETURNTRANSFER为true,确保cURL返回请求的响应内容而不是直接输出到页面 - 保留了原有的短信编码处理、参数拼接等核心业务逻辑,不会影响原本的短信发送功能
- 添加了异常捕获逻辑,方便排查请求过程中可能出现的问题
内容的提问来源于stack exchange,提问作者Omacy osei
相关产品推荐
相关产品推荐

