2012-03-10 4 views
-2

그래서 내 웹 사이트에 대해 트위터를 사용하는 사용자를 대상으로하는이 트위터 스크립트에서 작업을 시작했으며,이를 위해 고맙게 여기는 설정을 cron 작업과 함께 설정하려고합니다.배열 변수 값을 하나의 변수에 결합하는 방법

  1. 모든 15 명 사용자 이름을 통해 검색 페이지에서 사용자 이름 (총 15)

  2. 루프 구문 분석하고 그것 twitter.txt 파일 에있는 경우 스크립트를 중지합니다 : 이것은 어떻게 작동

  3. 사용자 이름이 twitter.txt 파일에 없으면 twitter.txt 파일에 사용자 이름을 쓰고 해당 사용자에게 트윗을 보냅니다.

그래서 내 문제는이 스크립트는 현재 단번에 15 개 트윗을 전송하고이 스팸으로 간주 될 수 있다는 점이다 (twitter.txt 파일은 동일한 사용자에게 중복 된 트윗을 보내는 방지 할 수 있습니다). 그래서 3 개의 트윗에 사용자 이름을 통합 할 수 있도록 도움이 필요합니다. 따라서 각 트윗에는 5 개의 사용자 이름이 포함됩니다. 트윗의 사용자 이름을 저장할 변수가있는 스크립트의 맨 아래에 표시됩니다.

//cURL 
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, "http://search.twitter.com/search.atom?q=MYWEBSITE"); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
$twitter = curl_exec($curl); 
curl_close($curl); 

//search html source page for user names 
preg_match_all('/\<uri\>http\:\/\/twitter\.com\/(.*?)\<\/uri\>/', $twitter, $usernames); 

//open the twitter text file and prepare for writing 
$twitter = fopen("twitter.txt", "a"); 

//contents of the twitter text file (holds the twitter user names) 
$contents = file_get_contents("twitter.txt"); 

//loop through each user name 
foreach($usernames[1] as $username) { 
    //if user name is found in the twitter text file it states "Found" and script stops 
    if (strpos($contents, $username) !== FALSE) { 
     echo $username . " - <font color=\"red\">Found</font><br />"; 
    //if user name is not found in the twitter text file it records the user name and tweets 
    } else { 
     //write to twitter text file 
     fwrite($twitter, $username."\r\n"); 

     //shows what user names were recorded 
     echo $username . "<br />"; 

     //user names for tweets 
     //this is where i need help dividing the usernames into these variables 
     $usernames_set_one = ""; 
     $usernames_set_two = ""; 
     $usernames_set_three = ""; 

     //tweet 
     $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET); 
     $content = $connection->get('account/verify_credentials'); 

     //twitter message 
     $connection->post('statuses/update', array('status' => $usernames_set_one . ' thank you for tweeting about my website.')); 
     $connection->post('statuses/update', array('status' => $usernames_set_two . ' thank you for tweeting about my website.')); 
     $connection->post('statuses/update', array('status' => $usernames_set_three . ' thank you for tweeting about my website.')); 

     return $connection; 
    } 

} 

//close the twitter text file no further writing 
fclose($twitter); 

답변

0

while loop 대신 foreach로를 사용하여 다음과 같이 사용자 이름을 액세스 할 수

$ 사용자 이름 [1] [$ i]는;

$ usernames [1] [$ i + 1];

$ usernames [1] [$ i + 2]; 예를 들어

:

$i = 0; 
$count = count($usernames[1]); 
while($i < $count - 3) //we will use $i+2 after all 
{ 

$i++; 
} 
0

변경은 그 3 개 변수 배열하고 첫 번째 세 번째로 그 제 2 어레이에 사용자 이름을 추가하고 차면. 트위터에 게시 할 때는 단순히 implode()으로 이름을 등록하십시오.

+0

나는 그 첫 번째 부분에 array_fill()을 사용할 것입니까? – jeff

+0

첫 번째 부분에서는'array_push ($ username_set_one, $ username)'을 사용할 수 있습니다.하지만이 배열 앞에 이미 세 개의 이름이 있는지 확인한 다음 다음 배열로 이동하십시오. 'count ($ username_set_one)'는 그 배열에있는 총 항목을 줄 것이다. – slash197

관련 문제