cobusbo Posted January 7, 2015 Share Posted January 7, 2015 (edited) Hi Ive currently developed a script that connects via an API to send a message, and tested it with one user and it works perfectly. But now im bit stuck to send it to multiple users. All my visitors unique IDs are being stored into a file called unique.csv all in 1 column under each other. But what im currently trying to do is to recall the info from the csv and make a contact list to broadcast the message to. The csv file looks like: 27823116700 m59317461002 27731503630 m61319393002 m36816579002 m38980833002 m12916668002 27712673085 m70046810004 m8095498002 m40363282002 m68756447002 m6865628002 m9087505002 m68741582002 m47489157002 Here is my current script <?php $f_pointer=fopen("unique.csv","r"); // file pointer while(! feof($f_pointer)){ $ar=fgetcsv($f_pointer); } require_once ('MxitAPI.php'); /* Instantiate the Mxit API */ $key = '****'; $secret = '*****'; $visitor = '****'; $api = new MxitAPI($key, $secret); $api->get_app_token('message/send'); $api->send_message('guniverse', '$ar[0]', '*test message*', 'true'); echo 'Success'; ?> Edited January 7, 2015 by cobusbo Quote Link to comment Share on other sites More sharing options...
wezhind Posted January 7, 2015 Share Posted January 7, 2015 I don't know if I'm missing something obvious here, but can't you just build an array from the initial while loop and then use foreach to loop through each of the id's sending the message to each? Or am I missing something? Quote Link to comment Share on other sites More sharing options...
cobusbo Posted January 7, 2015 Author Share Posted January 7, 2015 I don't know if I'm missing something obvious here, but can't you just build an array from the initial while loop and then use foreach to loop through each of the id's sending the message to each? Or am I missing something? you see the main problem is I need the 27823116700 m59317461002 27731503630 m61319393002 m36816579002 m38980833002 m12916668002 27712673085 m70046810004 m8095498002 m40363282002 m68756447002 m6865628002 m9087505002 m68741582002 m47489157002 stored as 27823116700,m59317461002,27731503630,m61319393002,m36816579002,m38980833002,m12916668002,27712673085,m70046810004,m8095498002,m40363282002,m68756447002,m6865628002,m9087505002,m68741582002,m47489157002 Quote Link to comment Share on other sites More sharing options...
Barand Posted January 7, 2015 Share Posted January 7, 2015 (edited) Store in array then join(). $f_pointer=fopen("unique.csv","r"); // file pointer $users = array(); while ($ar=fgetcsv($f_pointer)) { $users[] = $ar[0]; } fclose ($f_pointer); $list = join(',', $users); Edited January 7, 2015 by Barand 1 Quote Link to comment Share on other sites More sharing options...
wezhind Posted January 7, 2015 Share Posted January 7, 2015 I thought you wanted to send multiple messages? I'm not sure how having the userids comma delimited aids that...does the api you are using accept a comma delimited string of userids? Anyway, barand's solution will give you a string containing that - but let us know if that's not what you are intending. Good luck. Quote Link to comment Share on other sites More sharing options...
cobusbo Posted January 8, 2015 Author Share Posted January 8, 2015 (edited) Store in array then join(). $f_pointer=fopen("unique.csv","r"); // file pointer $users = array(); while ($ar=fgetcsv($f_pointer)) { $users[] = $ar[0]; } fclose ($f_pointer); $list = join(',', $users); Thank you, I just have another question regarding empty lines will it just ignore the line and move on to the next one or will it just stop where there is an empty line is in between? Since the API can send only 500 messages each time is it maybe possible to turn it into batches? and will it work if I point to the unique.csv file stored on another host? Edited January 8, 2015 by cobusbo Quote Link to comment Share on other sites More sharing options...
CroNiX Posted January 8, 2015 Share Posted January 8, 2015 It will continue through empty lines. The best way to learn is to look up the functions being used in the php manual, and also to try them out with test data to see. If you don't want it to store an empty line in the final output, then just check for it and don't add it to the array in the while() loop. if ($ar[0] != '') $users[] = $ar[0]; //only store line in new array if it contains something It *may* work retrieving the file from a different host using fopen(), but that relies on allow_url_fopen being enabled in your php.ini. Again, see the manual for fopen() for more details. You can always use CURL to retrieve from another site, which will work all of the time. 1 Quote Link to comment Share on other sites More sharing options...
wezhind Posted January 8, 2015 Share Posted January 8, 2015 Yes, it's definitely possible to turn it into batches. Various methods available to do so, but you will probably have to change the structure of your code a little to accommodate that. I suppose it depends on how many messages you are likely to send. If it's only a few multiples of 500 then you could probably store each batch in a separate array and then loop through the arrays and then loop through the content. If you're talking serious no.s though you may want to find a less memory intensive manner of achieving this I'd imagine. Good luck. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 8, 2015 Share Posted January 8, 2015 Easiest way to batch them is to use array_chunk(); $f_pointer=fopen("unique.csv","r"); // file pointer $users = array(); while ($ar=fgetcsv($f_pointer)) { if (trim($ar[0])!='') $users[] = $ar[0]; } fclose ($f_pointer); $batchSize = 50; // set size of your batches $batches = array_chunk($users, $batchSize); foreach ($batches as $batch) { $list = join(',', $batch); // process the batch list here } Quote Link to comment Share on other sites More sharing options...
cobusbo Posted January 10, 2015 Author Share Posted January 10, 2015 Easiest way to batch them is to use array_chunk(); $f_pointer=fopen("unique.csv","r"); // file pointer $users = array(); while ($ar=fgetcsv($f_pointer)) { if (trim($ar[0])!='') $users[] = $ar[0]; } fclose ($f_pointer); $batchSize = 50; // set size of your batches $batches = array_chunk($users, $batchSize); foreach ($batches as $batch) { $list = join(',', $batch); // process the batch list here } Ok but when I submit it, only the first 50 people gets the message is it possible to make it loop in batches of 50 till everyone on the list got the message? Quote Link to comment Share on other sites More sharing options...
Barand Posted January 10, 2015 Share Posted January 10, 2015 Are you sending each batch of 50 where I indicated, inside the foreach() loop? Quote Link to comment Share on other sites More sharing options...
cobusbo Posted January 10, 2015 Author Share Posted January 10, 2015 Are you sending each batch of 50 where I indicated, inside the foreach() loop? Yes I basically just copied the whole code and replaced it with mine. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 10, 2015 Share Posted January 10, 2015 Well if you replaced the whole of my code with yours then I cannot even know what it is doing now, let alone take any responsibility for it. Quote Link to comment Share on other sites More sharing options...
cobusbo Posted January 10, 2015 Author Share Posted January 10, 2015 Well if you replaced the whole of my code with yours then I cannot even know what it is doing now, let alone take any responsibility for it. Well what I'm trying to do is, my unique.csv file got like 1000 people listed, but I can only send a list of 50 people each time because the API I use can only support 50 names each time. So what im trying to do is split the list up in 50 so that it sends a batch of 50 messages and then send the next 50 till the total list is done. I've made my batchsize 2 to test it and only the first 2 people on my list got the message the other 998 people didnt. Here is the code I currently got <?php $visitor = $_GET["visitor"]; $f_pointer=fopen($visitor,"r"); // file pointer $users = array(); while ($ar=fgetcsv($f_pointer)) { if ($ar[0] != '') $users[] = $ar[0]; //only store line in new array if it contains something } fclose ($f_pointer); $batchSize = 2; // set size of your batches $batches = array_chunk($users, $batchSize); foreach ($batches as $batch) { $list = join(',', $batch); // process the batch list here } require_once ('MxitAPI.php'); /* Instantiate the Mxit API */ $key = $_GET["key"]; $secret = $_GET["secret"]; $app = $_GET["app"]; $message = $_GET["message"]; $api = new MxitAPI($key, $secret); $api->get_app_token('message/send'); $api->send_message($app, $list, $message, 'true'); echo 'Success'; ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted January 10, 2015 Share Posted January 10, 2015 The answer to my question in #11 above was "NO", not yes as you stated. You are only processing the final batch. Try something like this <?php $visitor = $_GET["visitor"]; $f_pointer=fopen($visitor,"r"); // file pointer $users = array(); while ($ar=fgetcsv($f_pointer)) { if ($ar[0] != '') $users[] = $ar[0]; //only store line in new array if it contains something } fclose ($f_pointer); $batchSize = 2; // set size of your batches $batches = array_chunk($users, $batchSize); require_once ('MxitAPI.php'); /* Instantiate the Mxit API */ $key = $_GET["key"]; $secret = $_GET["secret"]; $app = $_GET["app"]; $message = $_GET["message"]; $api = new MxitAPI($key, $secret); $api->get_app_token('message/send'); foreach ($batches as $batch) { $list = join(',', $batch); // process the batch list here $api->send_message($app, $list, $message, 'true'); } echo 'Success'; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.