0

Ive the following Code , a function take a list of usernames and put in them in array then execute function called function get_all_friends , Code works fine and no error , my question here how to adjust the code if ive bulk of usernames lets say like 10k ?

Like reading from file include all usernames name and put them in array using

$file_handle = fopen("users.txt")

please advise !

<?PHP
    $user1 = "usernamehere";
    $user2 = "usernamehere";
    $user3 = "usernamehere";
    $u[] = $user1;
    $u[] = $user2;
    $u[] = $user3;

    function get_all_friends($users) 
    {
        $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, TOKEN_KEY, TOKEN_SECRET);
        $list = array();
        foreach($users as $user)
        {
            $result = $connection->get( 'friends/ids', array(
            "screen_name"=> $user)
            );

            foreach($result->ids as $friend) 
            {
                $list[] = $friend;
            }

        }
        return $list;
    }

    //call the function
    $result = get_all_friends($u);

    foreach($result as $res)
    {
        $query = "INSERT INTO friends (userid, name, grade, flag) VALUES ($res, 'name', 100, 0 ) ";
        mysql_query($query);    
    }

    //to print the databse result
    echo "row<br />";
    $res = mysql_query("SELECT * FROM friends");  
    while ($row = mysql_fetch_assoc($res)) 
    {
        print_r($row);
    }
?>
Fluffeh
  • 33,228
  • 16
  • 67
  • 80
Jecki
  • 802
  • 3
  • 16
  • 32

2 Answers2

1

Something like the following should work (untested):

$filename = "users.txt";
$file_handle = fopen($filename, "r");
$contents = fread($file_handle, filesize($filename));
$usernames = preg_split("/ (,|\\n) /", $contents);
fclose($file_handle);

The usernames must be separated by a comma or a new line.

Although, if you are positive that the usernames will only be separated by a new line OR a comma, this code will be faster:

$filename = "users.txt";
$file_handle = fopen($filename, "r");
$contents = fread($file_handle, filesize($filename));
// new line:
$usernames = explode("\n", $contents);
// or comma:
$usernames = explode(",", $contents);
fclose($handle);

Please choose only one of the $usernames definitions.

AdamGold
  • 4,941
  • 4
  • 29
  • 47
  • sound good to me , but fclose($file_handle); should be after the function executed correct – Jecki Sep 17 '12 at 08:51
  • fclose should come when you're done with the file. If you don't do anything with the file in the get_all_friends function, then it should come before you call the function. – AdamGold Sep 17 '12 at 08:52
  • get_all_friends function is my main function i should take each username from the file and extract friends ids from twitter and add them to databases ( – Jecki Sep 17 '12 at 09:00
0

Specifically to answer the question, check file(..) - "Reads entire file into an array"

http://php.net/manual/en/function.file.php

Although you probably don't want to be doing it this way, if the file is large you'll be much better off reading sequentially or doing some sort of direct import.

moopet
  • 6,014
  • 1
  • 29
  • 36
  • suggest something for direct import , my file will like 10k line each line 1 username – Jecki Sep 17 '12 at 08:47
  • ok, see the answer here: http://stackoverflow.com/questions/3101021/insert-file-contents-into-mysql-tables-column – moopet Sep 17 '12 at 10:11