Reputation: 71
I've a SMS Api which send sms(not email) to my client with php. Now I'm trying to send sms from .txt file where all contact number exist:
My .txt file
shibbir, 8801678877639
babu, 8801534552503
So my while loop is look like this:
$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 );
But I get following error message:
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
My final goal is, User can send sms by uploading .txt file where all contact number exit. So once it's submit the form then sms must be send to those contact number's.
Thanks.
Upvotes: 1
Views: 75
Reputation: 1761
Try this.
$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 TEST
my.txt
shibbir, 8801678877639
babu, 8801534552503
my.php (removed the call to 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>";
?>
OUTPUT
Successfully sent your message to 8801678877639, 8801534552503
Thank You.
Upvotes: 1
Reputation: 8334
Change this line :
$line_of_text[1] ."<BR>";
It should be :
$line_of_text[1] .= "<BR>";
and also your message is inside your loop , so it will be echoed more than once.
This task can be done easily using file()
and explode()
Upvotes: 0
Reputation: 68536
1) Can you guys tell me why i get error message ?
2) And it's show 2 Success confirmation but i want it's must be show 1 success confirmation.
They are not errors.
Move the echo
DIV tag out of your while
loop.
Upvotes: 1
Reputation: 862
try changing your code into this
$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);
Upvotes: 1