1

我有一个 SMS Api,它使用 php 向我的客户发送短信(不是电子邮件)。现在我正在尝试从所有联系电话都存在的 .txt 文件中发送短信:

我的 .txt 文件

shibbir, 8801678877639
babu, 8801534552503

所以我的while循环是这样的:

$file_handle = fopen("$file", "r");
while ( !feof( $file_handle ) ) {
    $line_of_text = fgetcsv($file_handle, 1024);
    $line_of_text[1] ."<BR>";       
    $obj = new Sender("myservername","myport","username","password","$sender", "$msg", 
"$line_of_text","2","1"); 
    $obj->Submit();     
    echo "<div class='success'>Successfully sent your message to " . $line_of_text[1] . " Thank 
    You.</div>";
}
fclose( $file_handle );

但我收到以下错误消息:

Notice: Array to string conversion in D:\Software Installed\xampp\htdocs\evan\toplevel
\bulksms.php on line 108

Notice: Undefined offset: 1 in D:\Software Installed\xampp\htdocs\evan\toplevel\bulksms.php on 
line 106

Notice: Undefined offset: 1 in D:\Software Installed\xampp\htdocs\evan\toplevel\bulksms.php on 
line 107

我的最终目标是,用户可以通过上传所有联系号码退出的 .txt 文件来发送短信。因此,一旦提交表格,则必须将短信发送到这些联系号码。

  1. 你们能告诉我为什么我会收到错误消息吗?
  2. 它显示 2 成功确认,但我希望它必须显示 1 成功确认。

谢谢。

4

4 回答 4

1

1)你们能告诉我为什么我收到错误消息吗?
2) 它显示 2 成功确认,但我希望它必须显示 1 成功确认。

  1. 它们不是错误。

  2. echoDIV 标签移出while循环。

于 2013-09-17T08:01:00.387 回答
1

尝试将您的代码更改为此

$file_handle = fopen("$file", "r");
while (!feof($file_handle) )
{
$line_of_text = fgetcsv($file_handle, 1024);
$line_of_text[1]=$line_of_text[1] ."<BR>";       
$obj = new Sender("myservername","myport","username","password","$sender", "$msg", 
$line_of_text[1],"2","1"); 
$obj->Submit();     
echo "<div class='success'>Successfully sent your message to " . $line_of_text[1] . " Thank 
You.</div>";
}
fclose($file_handle);
于 2013-09-17T08:00:15.790 回答
1

尝试这个。

$file_handle = fopen("$file", "r");
$success_number = array();
    while ( !feof( $file_handle ) ) {
        $line_of_text = fgetcsv($file_handle, 1024);
        $success_number[]=$line_of_text[1]; //call this statement if msg sent successfully.
        $line_of_text  = implode(',',$line_of_text ); // i think we need to pass string to the api.
        $obj = new Sender("myservername","myport","username","password","$sender", "$msg", 
    "$line_of_text","2","1"); 
        $obj->Submit();     
    }
    echo "<div class='success'>Successfully sent your message to ".implode(',',$success_number) ."<BR> Thank You.</div>";
    fclose( $file_handle );

我的测试

我的.txt

shibbir, 8801678877639
babu, 8801534552503

my.php(删除了对 api 的调用)

    <?php
    $file = "my.txt";
    $file_handle = fopen($file, "r");
    $success_number = array();
    while ( !feof( $file_handle ) ) {
        $line_of_text = fgetcsv($file_handle, 1024);    
        $success_number[]=$line_of_text[1]; //call this statement if msg sent successfully.
    }
    fclose( $file_handle );
    echo "<div class='success'>Successfully sent your message to " . implode(',',$success_number) ."<BR> Thank You.</div>";
?>

输出

    Successfully sent your message to 8801678877639, 8801534552503
Thank You.
于 2013-09-17T08:05:08.993 回答
0

更改此行:

$line_of_text[1] ."<BR>";  

它应该是 :

$line_of_text[1] .= "<BR>";       

而且您的消息也在您的循环中,因此它会被多次回显。

这个任务可以很容易地使用file()explode()

于 2013-09-17T08:03:31.873 回答