1

我正在寻找使用 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

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

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

有任何想法吗?

4

1 回答 1

4

做你想做的事,你必须:

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

  • 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;
    }
}
于 2013-09-17T16:26:25.520 回答