PHP 将一个函数的结果获取到另一个函数中


PHP get the results of a function into another

我有这个函数

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){}  
}

我该怎么做??

谢谢

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']);
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']访问这些值。

正如上面的人所说,

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)