2012-02-08 50 views
0

我有一个数字的数据库,我想用短信提供商发送短信。我的代码与sms api一起,我作为新手所遇到的问题是他们的代码是针对单个短信的,我想循环访问我的数据库,并通过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!"; 


} 

回答

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"); 

} 
+0

非常感谢Arkouda,我感到非常愚蠢,因为我确信我尝试过,并没有奏效 - 他一直在看屏幕太久! – Textus 2012-02-08 18:12:20

0

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