2014-02-12 3 views
-1
<?php 
require 'app_tokens2.php'; 
require 'tmhOAuth-master/tmhOAuth.php'; 

$query = htmlspecialchars($_GET['query']); 
if (empty($query)) 
{ 
    $query = "pitbull"; 
} 
$connection = new tmhOAuth(array(
    'consumer_key' => $consumer_key, 
    'consumer_secret' => $consumer_secret, 
    'user_token' => $user_token, 
    'user_secret' => $user_secret 
)); 

// Get the timeline with the Twitter API 
$http_code = $connection->request('GET',$connection->url('1.1/search/tweets'), 
    array('q' => $query,'count' => 4, 'lang' => 'en')); 

// Request was successful 
if ($http_code == 200) 
{ 
    // Extract the tweets from the API response 
    $response = json_decode($connection->response['response'],true); 
    $tweet_data = $response['statuses']; 
    // Accumulate tweets from results 
    $tweet_stream = '['; 

    foreach ($tweet_data as $tweet) 
    { 
     // Add this tweet's text to the results 
     $tweet_stream .= '{ "tweet":' . json_encode($tweet['text']) . ' },'; 
    } 

    $tweet_stream = substr($tweet_stream, 0, -1); 
    $tweet_stream .= ']'; 

    // Send the tweets back to the Ajax request 
    print $tweet_stream; 

    // Connect to Mongo and set DB and Collection 
    $mongo = new Mongo(); 
    $db = $mongo->sample1; 
    $collection = $db->trial3; 

    // Convert JSON to a PHP array 
    $tweet_stream = json_decode($tweet_stream,true); 

    // Loop array and create seperate documents for each tweet 
    foreach ($tweet_stream as $item) 
    { 
     $collection->insert($item); 
    } 

    // fetch all tweets from the collection 
    $posts = $collection->find(); 
    foreach ($posts as $post) { 
     // display the posts 
     //print $post; 
     print_r($post);  //print_r prints a variable in a more human-readable form: 
    } 
    var_dump($collection->count());  //counts number of documents in a collection 
} 
// Handle errors from API request 
else 
{ 
    if ($http_code == 429) 
    { 
     print 'Error: Twitter API rate limit reached'; 
    } 
    else 
    { 
     print 'Error: Twitter was not able to process that request'; 
    } 
} 

위의 코드가 올바르게 작동합니다. 그러나 문제는 내가 post 방법을 사용하는 경우는 내가 포스트 방법 오류가있는 교체를 노력하고있어 지오 태깅 tweets.But을 얻을 포스트 방법을 사용하려는 working.Gives 오류 메시지twitter API의 POST 메서드가 작동하지 않습니다.

'Error: Twitter was not able to process that request'

가 없습니다 주어진다.

+0

_ "하지만 대체물을 게시 할 때 _ _ 우리에게 _how_을 (를) 보여 주려고했습니다. – CBroe

답변

0

POST 방법을 사용 하시겠습니까? 검색 쿼리에 사용하려는 경우에는 사용할 수 없습니다.

검색 트윗의 Documentation을보세요. POST 방법을 사용할 수 없습니다. 검색 트윗의 경우 GET HTTP 메소드 만 사용할 수 있습니다.

+0

위도와 경도로 트윗을 추출하려면 post 메소드를 사용해야합니다. – user3219417

관련 문제