【问题标题】:Curl to sms provider looping through database卷曲到通过数据库循环的短信提供商
【发布时间】:2012-02-08 17:19:01
【问题描述】:

我有一个数字数据库,我想使用短信提供商发送短信。我的代码与 sms api 一起在这里,我作为新手遇到的问题是他们的代码是针对单个 sms 的,我想循环访问我的数据库并通过 curl 为我的数据库中的每个数字发布。

非常感谢任何帮助。

$result = mysql_query($stremail);
$emails = array();
while ($row = mysql_fetch_array($result)) {
$recipient =  $row['mobilenumber1'];

class SendSMS
{
private $url = 'http://'; // url of the service
private $username = ''; // 
private $password = ''; // 

private $message_id,$credits_used;

function __construct()
{

}

public function getMessageID()
{
    return $this->message_id;
}

public function getCreditsUsed()
{
    return $this->credits_used;
}


// public function to commit the send
public function send($message,$recipient,$originator)
{
    $url_array= array('message'=>$message,'mobile_number'=>$recipient,'originator'=>$originator,'username'=>$this->username, 'password'=>$this->password);
    $url_string = $data = http_build_query($url_array, '', '&');

    // we're using the curl library to make the request
    $curlHandle = curl_init();
    curl_setopt($curlHandle, CURLOPT_URL, $this->url);
    curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $url_string);
    curl_setopt($curlHandle, CURLOPT_POST, 1);
    $responseBody = curl_exec($curlHandle);
    $responseInfo  = curl_getinfo($curlHandle);
    curl_close($curlHandle);

    return $this->handleResponse($responseBody,$responseInfo);
}


private function handleResponse($body,$info)
{
    if ($info['http_code']==200){ // successful submission
        $xml_obj = simplexml_load_string($body);
        // extract message id and credit usuage
        $this->message_id = (int) $xml_obj->message_id;
        $this->credits_used = (int) $xml_obj->credits_used;
        return true;
    }
    else{

        $this->message_id = null;
        $this->credits_used = null;

        // error handling
        return false;
    }

}

}
$sms = new SendSMS();
$sms->send($message1,$recipient,"header");
echo "sent!";


}

【问题讨论】:

    标签: php mysql curl


    【解决方案1】:
    class SendSMS
    {
        private $url = 'http://'; // url of the service
        private $username = ''; // 
        private $password = ''; // 
    
        private $message_id,$credits_used;
    
        function __construct()
        {
    
        }
    
        public function getMessageID()
        {
            return $this->message_id;
        }
    
        public function getCreditsUsed()
        {
            return $this->credits_used;
        }
    
    
        // public function to commit the send
        public function send($message,$recipient,$originator)
        {
            $url_array= array('message'=>$message,'mobile_number'=>$recipient,'originator'=>$originator,'username'=>$this->username, 'password'=>$this->password);
            $url_string = $data = http_build_query($url_array, '', '&');
    
            // we're using the curl library to make the request
            $curlHandle = curl_init();
            curl_setopt($curlHandle, CURLOPT_URL, $this->url);
            curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $url_string);
            curl_setopt($curlHandle, CURLOPT_POST, 1);
            $responseBody = curl_exec($curlHandle);
            $responseInfo  = curl_getinfo($curlHandle);
            curl_close($curlHandle);
    
            return $this->handleResponse($responseBody,$responseInfo);
        }
    
    
        private function handleResponse($body,$info)
        {
            if ($info['http_code']==200){ // successful submission
                $xml_obj = simplexml_load_string($body);
                // extract message id and credit usuage
                $this->message_id = (int) $xml_obj->message_id;
                $this->credits_used = (int) $xml_obj->credits_used;
                return true;
            }
            else{
    
                $this->message_id = null;
                $this->credits_used = null;
    
                // error handling
                return false;
            }
    
        }
    
    }
    
    $sms = new SendSMS();
    
    $result = mysql_query($stremail);
    
    while ($row = mysql_fetch_array($result)) {
    
        $recipient = $row['mobilenumber1'];
        $sms->send($message1,$recipient,"header");
    
    }
    

    【讨论】:

    • 非常感谢 Arkouda,我觉得自己很愚蠢,因为我确定我试过了,但它没有用 - 呵呵,看屏幕太久了!
    【解决方案2】:

    如果您的短信提供商不允许您通过 API 接口发送批量短信,我推荐您使用 HQSMS.com 短信服务。 在 HQSMS 代码中,您只需将变量 $ 分配给以逗号分隔的更多数字。在单个 POST 请求中,最多可能有 10000 个数字。您可以在http://www.hqsms.com/help-api/https-api-interface/https-specification 上找到更多详细信息和示例。 HQSMS.com 是一家高质量的 SMS 提供商,可为您提供具有开放式有效性和全球覆盖范围的短信。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-17
      • 2019-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多