while循环中的Php错误问题

时间:2013-09-17 07:53:58

标签: php

我有一个短信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成功确认。
  3. 感谢。

4 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

  

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

  1. 他们错误。

  2. echo DIV标记移出 while 循环。

答案 2 :(得分: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 );

我的测试

My.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.

答案 3 :(得分:0)

更改此行:

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

应该是:

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

并且您的消息也在您的循环中,因此它将不止一次回显。

使用file()explode()

可以轻松完成此任务