PHP将函数的结果转换为另一个函数

时间:2011-12-26 16:26:22

标签: php function

我有这个功能

function getTwitterAuth($user_id) {
$d = "SELECT * FROM `twitterAccounts` WHERE `user_id`='".$user_id."'";
$dr=mysql_query($d) or die("Error selecting twitter account: ".mysql_error());
$drow = mysql_fetch_assoc($dr);
**$twitter_auth_token** = $drow['oauth_token'];
**$twitter_auth_secret** = $drow['oauth_token_secret']
}

它会告诉我两个变量的结果,然后我需要传递给另一个函数:

function twitterReply($twitter_message, $reply_to_id) {
$twitterObj->setToken(**$twitter_auth_token**, **$twitter_auth_secret**);
$twitter_user = $twitterObj->get_accountVerify_credentials();
try{  
$twitter_user->id;
$twitterObj->post_statusesUpdate(array("status" => $message, "in_reply_to_status_id" => $reply_to_id); 
//echo "done";
}
catch(EpiTwitterException $e){}  
}

我该怎么办?

非常感谢

3 个答案:

答案 0 :(得分:2)

function getTwitterAuth($user_id) {
  $d = "SELECT * FROM `twitterAccounts` WHERE `user_id`='".$user_id."'";
  $dr=mysql_query($d) or die("Error selecting twitter account: ".mysql_error());
  $drow = mysql_fetch_assoc($dr);
  $twitter_auth_token = $drow['oauth_token'];
  $twitter_auth_secret = $drow['oauth_token_secret'];
  return Array("token" =>  $twitter_auth_token, "secret" => $twitter_auth_secret);
}

function twitterReply($twitter_auth_token, $twitter_auth_secret, $twitter_message, $reply_to_id) {
  $twitterObj->setToken($twitter_auth_token, $twitter_auth_secret);
  $twitter_user = $twitterObj->get_accountVerify_credentials();
  try{  
    $twitter_user->id;
    $twitterObj->post_statusesUpdate(array("status" => $message, "in_reply_to_status_id" => $reply_to_id); 
    //echo "done";
  }
    catch(EpiTwitterException $e){}  
}

$res = getTwitterAuth($user_id);
twitterReply($res["token"], $res["secret"], $twitter_message, $reply_to_id);

编辑:如在另一个答案设置中所见,$ twitter_auth_token和$ twitter_auth_secret是多余的,函数getTwitterAuth的最后三行可以追加到:

return Array("token" =>  $drow['oauth_token'], "secret" => $drow['oauth_token_secret']);

答案 1 :(得分:1)

function getTwitterAuth($user_id) {

$d = "SELECT * FROM `twitterAccounts` WHERE `user_id`='".$user_id."'";
$dr=mysql_query($d) or die("Error selecting twitter account: ".mysql_error());
$drow = mysql_fetch_assoc($dr);
$array = array();
$array['twitter_auth_token'] = $drow['oauth_token'];
$array['twitter_auth_secret'] = $drow['oauth_token_secret'];
return $array
}

$twitterTokens = getTwitterAuth($user_id);

现在你可以使用$ twitterTokens ['twitter_auth_token']和$ twitterTokens ['twitter_auth_secret']

来访问这些值

答案 2 :(得分:0)

如上所述,

function getTwitterAuth($user_id) {
....
**$twitter['auth_token']** = $drow['oauth_token'];
**$twitter['auth_secret']** = $drow['oauth_token_secret'];
return($twitter);
}

function twitterReply($twitter,$twitter_message, $reply_to_id) {
$twitterObj->setToken(**$twitter['auth_token']**, **$twitter['auth_secret']**);
...
}

$twitter_info = getTwitterAuth($user_id);
twitterReply($twitter_info, $twitter_message, $reply_to_id)