Jump to content

Recommended Posts

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 by cobusbo
Link to comment
https://forums.phpfreaks.com/topic/293721-getting-info-from-csv-file/
Share on other sites

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

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 by Barand
  • Like 1

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.

 

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 by cobusbo

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.

  • Like 1

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.

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
}

 

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?

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';
?>

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';
?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.