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);
    }
?>
1
  • check working example on official site: php.net/fopen Commented Sep 17, 2012 at 8:40

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

sound good to me , but fclose($file_handle); should be after the function executed correct
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.
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 (
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.

2 Comments

suggest something for direct import , my file will like 10k line each line 1 username

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.