0

我一直在尝试使用下面的代码发送短信,但是当我循环时它没有发送。仅当我从数据库中选择一个数字时它才有效。我在数据库中有超过 5,000 个号码,并希望同时向所有号码发送短信,请帮助。

mysql_select_db($database_xxx, $xxx);
$query_rs = "SELECT phone FROM `notify` order by id asc LIMIT $l1 , $l2";
$rs= mysql_query($query_rs, $xxx) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs= mysql_num_rows($rs);

$phone = $row_rs['phone'];

// Do while loop to send sms.
while($row=mysql_fetch_assoc($rs)){


                         // Let's do some formatting and keep smiling.
    $giringirin = ereg_replace("[^0-9]", "", $phone );
    if (strlen($giringirin) == 11) { 
    $phone1=substr($giringirin, 1);
    $phone= "234$phone1";
    } elseif (strlen($giringirin) == 13){ 
    $phone = $giringirin; 
    }

extract($_POST);


//set POST variables
$url = "http://sms.xxx.com/bulksms/bulksms.php?username=$username&password=$password&message=$smsmessage&mobile=$phone&sender=$sender";
$fields = array(
        );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);

if ($result == '1801') { echo "SMS has also been sent to the Customer ($phone) \n";} else { echo "Oooops, No sms was sent";}

//close connection
curl_close($ch);  
}
4

2 回答 2

0

您的代码令人困惑...

curl_setopt($ch,CURLOPT_POST,count($fields));

CURLOPT_POST 是一个布尔标志。要么你在做一个帖子,要么你不是。您发布的字段数量无关紧要。

您正在构建一系列要发布的变量/值,但通过字符串操作来完成。CURL 完全能够获取一个数组并为您完成所有这些工作,将您的整个 foreach 循环减少到仅

curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

您正在使用extract()on $_POST,这会污染您脚本的变量名称空间,并带有恶意用户想要发送的任何垃圾 - 您实际上是register_globals在再次复制 PHP 完全愚蠢的脑死亡。

您正在使用ereg,它已被弃用了大约 500 万互联网年。您应该改用这些preg功能

于 2012-08-13T04:21:47.470 回答
-1

当您在浏览器中运行此脚本时会发生什么?空白页?某物?错误?

从改变开始

$url = "http://sms.xxx.com/bulksms/bulksms.php?username=$username&password=$password&message=$smsmessage&mobile=$phone&sender=$sender";

$url = "http://sms.xxx.com/bulksms/bulksms.php?username=".$username."&password=".$password."&message=".$smsmessage."&mobile=".$phone."&sender=".$sender."";
于 2012-08-13T04:19:40.527 回答