Issue with sending direct messages - account only gets 1 of the direct messages twice


#1

Hey guys I am having an issue with sending emails out with Twitter API.

Issue: I have a app that allows people to add their accounts to a database that will check every single account that they layout to check status for certain values and if true send out a direct message specific to what they make the direct message to be. Everything works when it comes to finding if an account has a value or not but my issue is when it comes to sending out a direct message, it sends the direct message of one person twice and nothing happens with the other messages?

so:

Joe, Bob, David and watch P twitter account. P makes a post…“I like pie” Joe, bob, David send a direct message and Joe says “Hi, I’m Joe”, bob said “Hi, I’m bob”, and David said “Hi, I’m David.” The account that is being watch, P gets only bob’s direct message twice and the rest are unseen.

Code:

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


function confirm($x, $y, $z,$h,$currentTwitt,$newDBH)
		{
			
			$twittacc = $currentTwitt;
			$inerlevelWatch = $x;
			$NewDirectMessage .= $h . " " .$y;
			$parameters = array('screen_name' => $inerlevelWatch, 'text' => $NewDirectMessage);
			$method = 'direct_messages/new';
			$z->post($method, $parameters);
	
			
			$wsql = "UPDATE twitter_accounts SET hashtag_used =:hash WHERE username = :user";
			$q = $newDBH->prepare($wsql);
			$q->execute(array(':hash' => $h,':user'=>$twittacc));
		}

$Directmessage;
//run through each twitter account and see if message has been posted by the watch accounts
$sql = "SELECT username,oauth_token,oauth_secret,message FROM twitter_accounts";
$stmt = $DBH->prepare($sql);  
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $check = $row;
	$Directmessage = $check['message'];
	$currentTwitter = $check['username'];
	$currentTwitteroauth = $check['oauth_token'];
	$currentTwitteroauthsecret = $check['oauth_secret'];
	//make object
	$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $currentTwitteroauth, $currentTwitteroauthsecret);
	//make connection
	$user = $connection->get('account/verify_credentials');
	
	$content = $connection->get('account/rate_limit_status');
	echo "Current API hits remaining: {$content->remaining_hits}.";
	//get the accounts to watch and see if any have posted the correct format
	$tsql = "SELECT username FROM watch_accounts";
	$tstmt = $DBH->prepare($tsql);  
	$tstmt->execute();
	while ($trow = $tstmt->fetch(PDO::FETCH_ASSOC)) {
		$tcheck = $trow;
		$watchAccount = $tcheck['username'];
		trim($watchAccount);
		//get watch account info
		$watchAccInfo = $connection->get('users/show/'.$watchAccount.'');
		//grab the status text
		$currentStatus = $watchAccInfo->status->text;
		
		//grab the second # with text added to it.
		$pos1 = preg_match( "/.*(#\S+)/", $currentStatus , $match );
		$hash = $match[1];
		// make sure hash was not used before
		$result = $DBH->prepare("SELECT hashtag_used FROM twitter_accounts WHERE username = :user");
		$result->execute(array(':user' => $currentTwitter));
		while ($qrow = $result->fetch(PDO::FETCH_ASSOC)) {
				$check_hash = $qrow['hashtag_used'];
				if($check_hash == trim($hash)){
					$valid = 0;
				}
				else
					$valid = 1;
		}
		//check to make sure it is proper tweet being made
		if (strpos($currentStatus, 'in sizes') !== false && $valid == 1)
		{
				confirm($watchAccount, $Directmessage, $connection,$hash,$currentTwitter,$DBH);
		}
		else if (strpos($currentStatus, 'sizes') !== false && $valid == 1)
		{
				confirm($watchAccount, $Directmessage, $connection,$hash,$currentTwitter,$DBH);
		}
		
	}
}
$DBH = null;

#2