【问题标题】:Delete a tweet using the twitter-async library by searching for the tweet?通过搜索推文使用 twitter-async 库删除推文?
【发布时间】:2013-09-17 08:16:50
【问题描述】:

我正在寻找使用 twitter-async 从https://github.com/jmathai/twitter-async 删除推文的帮助,我猜想搜索推文?

如果我们尝试以下代码,我们可以发布到 twitter

try {
    $twitter->post_statusesUpdate(array('status' => $tweet));
} catch (EpiTwitterForbiddenException $e) {
    $msg = json_decode($e->getMessage());
    if ($msg->error != 'Status is a duplicate.') {
    //throw $e;
    }
}

https://dev.twitter.com/docs/api/1.1/post/statuses/destroy/%3Aid

但是,如果它第二次运行两次,它将返回它是一条重复的推文……或者如果它在之前发布了几条推文,它将再次返回它是一条重复的推文。

如何搜索然后删除或直接删除重复的推文,然后再次推特确切的消息(将其放在顶部/最新推文)

有什么想法吗?

【问题讨论】:

    标签: php json twitter twitter-oauth


    【解决方案1】:

    要做你想做的事,你必须:

    • 1st:搜索用户的tweet,如果有重复tweet,则解释其json得到重复tweet的id。 (请注意,在比较文本时,您应使用 php 函数 htmlspecialchars(),因为在 twitter 中存储了一些特殊字符作为 HTML 实体 [ref.]);

    • 2nd:如果存在重复推文,则删除;

    • 第三次:(重新)发布推文。

    (您可以选择添加第 0 步,即尝试正常提交推文,只有在出现错误时才继续执行其他步骤,这取决于您。)

    这里有一段代码可以发出这些请求并解释搜索的 json 等:

    $settings = array(
        'oauth_access_token' => "...",
        'oauth_access_token_secret' => "...",
        'consumer_key' => "...",
        'consumer_secret' => "..."
    );
    
    $API = new twitter_API($settings);
    
    $tweet_text = '>>testing the twitter API-1.1...';
    
    
    ## search the list of tweets for a duplicate...
    $url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
    $json = $API->make_request($url, "GET");
    
    $twitter_data = json_decode($json);
    
    $id_str = null;
    foreach ($twitter_data as $item){
        $cur_text = $item->text;
    
        if (strcmp($cur_text, htmlspecialchars($tweet_text))==0){
            $id_str = $item->id_str;
            echo "found a duplicate tweet with the id: " . $id_str . "<br /><br />";
        }
    }
    
    
    ## remove the duplicate, if there is one...
    if ($id_str){
        $url = "https://api.twitter.com/1.1/statuses/destroy/" . $id_str . ".json";
        $json = $API->make_request($url, "POST");
        echo $json . '<br /><br />';
    }
    
    
    ## post the tweet
    $url = "https://api.twitter.com/1.1/statuses/update.json";
    $postfields = array(
        'status' => $tweet_text
    );
    $json = $API->make_request($url, "POST", $postfields);
    echo $json . '<br /><br />';
    

    此代码使用类 twitter_API,它改编自 [ref.] 中的答案。你可以使用这个类,或者用 twitter-async 的函数替换对它们的函数的调用。

    class twitter_API
    {
        private $oauth_access_token;
        private $oauth_access_token_secret;
        private $consumer_key;
        private $consumer_secret;
        protected $oauth;
    
        public function __construct(array $settings){
            if (!in_array('curl', get_loaded_extensions())){
                echo 'you need to install cURL!';
                exit();
            }
            $this->oauth_access_token = $settings['oauth_access_token'];
            $this->oauth_access_token_secret = $settings['oauth_access_token_secret'];
            $this->consumer_key = $settings['consumer_key'];
            $this->consumer_secret = $settings['consumer_secret'];
        }
    
        function build_base_string($base_URI, $method, $params){
            $r = array();
            ksort($params);
            foreach($params as $key=>$value){
                $r[] = "$key=" . rawurlencode($value);
            }
            return $method . "&" . rawurlencode($base_URI) . '&' . rawurlencode(implode('&', $r));
        }
    
        function build_authorization_header($oauth){
            $r = 'authorization: oauth ';
            $values = array();
            foreach ($oauth as $key=>$value)
                $values[] = "$key=\"" . rawurlencode($value) . "\"";
            $r .= implode(', ', $values);
            return $r;
        }
    
        function make_request($url, $type, $args=null){
            $this->oauth = array( 'oauth_consumer_key' => $this->consumer_key,
                            'oauth_nonce' => time(),
                            'oauth_signature_method' => 'HMAC-SHA1',
                            'oauth_token' => $this->oauth_access_token,
                            'oauth_timestamp' => time(),
                            'oauth_version' => '1.0');
    
            if (($type=="GET") && (!is_null($args))){
                $getfields = str_replace('?', '', explode('&', $args));
                foreach ($getfields as $field){
                    $field_strs = explode('=', $field);
                    $this->oauth[$field_strs[0]] = $field_strs[1];
                }
            }
    
            $base_info = $this->build_base_string($url, $type, $this->oauth);
            $composite_key = rawurlencode($this->consumer_secret) . '&' . rawurlencode($this->oauth_access_token_secret);
            $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
            $this->oauth['oauth_signature'] = $oauth_signature;
    
            // make request
            $header = array($this->build_authorization_header($this->oauth), 'expect:');
            $options = array( CURLOPT_HTTPHEADER => $header,
                              CURLOPT_HEADER => false,
                              CURLOPT_URL => $url,
                              CURLOPT_RETURNTRANSFER => true,
                              CURLOPT_SSL_VERIFYPEER => false);
            if ($type=="POST"){
                if (is_null($args)){
                    $args = array();
                }
                $options[CURLOPT_POSTFIELDS] = $args;
            }
            else if (($type=="GET") && (!is_null($args))){
                $options[CURLOPT_URL] .= $args;
            }
    
            $feed = curl_init();
            curl_setopt_array($feed, $options);
            $json = curl_exec($feed);
            curl_close($feed);
    
            return $json;
        }
    }
    

    【讨论】:

    • 如果您需要在搜索结果中获得比默认值更多的推文,只需替换以下行:'$json = $API->make_request($url, "GET");'通过'$json = $API->make_request($url, "GET", "?count=" . $n);'其中 $n 是您想要获取的推文数量。
    猜你喜欢
    • 2013-05-30
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 2014-06-30
    • 2013-08-19
    • 2016-06-26
    • 2021-07-25
    相关资源
    最近更新 更多