0

我通过 $_GET 使用此脚本将用户名添加到我的数据库旁边到我的 Twitter 列表中脚本功能很好
,例如当我在 url domain.com/add.php?user=username 调用它时添加执行 100% 的功能很好
我的疑问是关于调整代码以通过数组一次添加多个用户或从文件中获取用户列表有任何提示来调整代码或修改它吗?

 <?php
session_start();

 require_once('dbconnect.php');
  require_once('twitteroauth/twitteroauth.php');
   require_once('config.php');

   $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET,TOKEN_KEY, TOKEN_SECRET);

   $user = $_GET['user'];

 if( !isset( $_GET['user'] ) )
die('You must enter a username');


   $info = $connection->get( 'users/show', array("screen_name"=> $user
                                  ));   
$var = $_GET['user'];



 $individual = 1;

 $protected = ($info->protected == "true")?1:0;

$query = sprintf("INSERT INTO tweeps VALUES('%s', '%s', %s, %s, '%s', '%s', '%s', %s ) ",
mysql_real_escape_string($info->screen_name),
mysql_real_escape_string($info->name),
$info->followers_count,
$info->statuses_count,
mysql_real_escape_string($info->location),
$info->created_at,
$info->profile_image_url,
$protected)    ;


  echo $query;      

$result = mysql_query($query); 
if(false) 
 echo "s";

 else {

$list_id = get_latest_list();
$list = "Tweeps-" . $list_id;

    echo "list $list";

mysql_free_result($result);
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, TOKEN_KEY, TOKEN_SECRET);

$result = $connection->post( 'friendships/create/' .  $info->screen_name    );  
//var_dump($result); die();
    $result = $connection->post( 'lists/members/create', array("screen_name"=>   $info->screen_name,
                                   "slug"=> $list,
                                       "owner_screen_name"=>  "Tweeps")  ); 

if( !isset($result->error) ) {

    $userid = $info->id;
    $result = mysql_query("INSERT INTO followed_tweeps(screenname, userid, individual, list_id) VALUES('" . $info->screen_name . "', '" . $userid . "', $individual, $list_id)");       

}

 }
    echo "SUCCESS";





  function get_latest_list() {

$query = "SELECT count(*) cnt, list_id FROM followed_tweeps group by list_id order by  list_id desc";

$result = mysql_query($query);

if(! $result ) {
    die("Error: query is $query error is: " . mysql_error() );
}

$row = mysql_fetch_row($result);

    $cnt = $row[0];
    $free_slots = 500 - $cnt;   
    $list_id = ($freeslots > 0) ? $row[1]++ : $row[1];
    return $list_id;

  }


  ?>
4

1 回答 1

6

PHP 支持多个同名查询参数的数组表示法:

<input type="text" name="name[]" />
                             ^^--- tells PHP to treat 'name' as an array

然后,您可以将其处理为:

if (isset($_GET['name']) && isarray($_GET['name'])) {
   foreach ($_GET['name'] as $name) {
      ...
   }
}

这对 POST 和 GET 都有效 - 您只需[]在字段名称中包含 。

于 2012-08-13T22:18:23.990 回答