while循环中的Php错误问题


Php error issue in while loop

我有一个短信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 成功确认,
  3. 但我希望它必须是显示 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.

尝试将代码更改为

$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)你们能告诉我为什么我收到错误消息吗?2
)它是节目2 成功确认,但我希望它必须显示 1 个成功 确认。

  1. 它们不是错误。

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

更改此行:

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

它应该是:

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

而且你的消息在你的循环内,所以它会被回响不止一次。

可以使用file()explode()轻松完成此任务