To do what you want, you have to:
1st: search the tweets of the user, and interpret its json to get the id of a repeated tweet if there is one. (notice that when comparing the text you shall use the php function htmlspecialchars() because there are special characters that are stored in twitter as HTML entities [ref.]);
2nd: remove the repeated tweet if it exists;
3rd: (re-)post the tweet.
(optionally you can add a 0th step, which would be to try a normal submission of the tweet, and advance to the other steps only if you have an error, it's up to you.)
Here you have a code that makes these requests and interpret the json of the search, etc.:
$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 />';
This code uses the class twitter_API, which is an adaptation from the answers in [ref.]. You can use this class, or replace the callings to their functions by the functions of 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;
}
}