Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过搜索tweet,使用twitter异步库删除tweet?_Php_Json_Twitter_Twitter Oauth - Fatal编程技术网

Php 通过搜索tweet,使用twitter异步库删除tweet?

Php 通过搜索tweet,使用twitter异步库删除tweet?,php,json,twitter,twitter-oauth,Php,Json,Twitter,Twitter Oauth,我正在寻找一些帮助删除推文使用twitter异步从我猜搜索推文 如果我们尝试以下代码,我们可以发布到twitter try { $twitter->post_statusesUpdate(array('status' => $tweet)); } catch (EpiTwitterForbiddenException $e) { $msg = json_decode($e->getMessage()); if ($msg->error != 'St

我正在寻找一些帮助删除推文使用twitter异步从我猜搜索推文

如果我们尝试以下代码,我们可以发布到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;
    }
}
但是,如果它第二次运行两次,它将返回重复的tweet。。。或者,如果之前有人发了几条tweet,它会再次返回重复的tweet

我如何搜索然后删除,或者直接删除重复的tweet,然后再次发布准确的消息,将其放在最新tweet上


有什么想法吗?

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

第一:搜索用户的tweet,并解释其json以获取重复tweet的id(如果有)。请注意,在比较文本时,您应该使用php函数htmlspecialchars,因为twitter中有一些特殊字符存储为HTML实体

第二:删除重复的tweet(如果存在)

第三:重新发布推文

或者,您可以添加第0步,这将是尝试正常提交tweet,并且只有在出现错误时才进入其他步骤,这取决于您自己

这里有一段代码,用于发出这些请求并解释搜索的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 />';

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

第一:搜索用户的tweet,并解释其json以获取重复tweet的id(如果有)。请注意,在比较文本时,您应该使用php函数htmlspecialchars,因为twitter中有一些特殊字符存储为HTML实体

第二:删除重复的tweet(如果存在)

第三:重新发布推文

或者,您可以添加第0步,这将是尝试正常提交tweet,并且只有在出现错误时才进入其他步骤,这取决于您自己

这里有一段代码,用于发出这些请求并解释搜索的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 />';

如果需要在搜索结果中获得比默认值更多的tweet,只需替换以下行:“$json=$API->make_request$url,get;”通过“$json=$API->make_request$url,GET,?count=”$n、 "其中$n是您想要获取的推文数量。如果您需要在搜索结果中获取比默认值更多的推文,只需替换以下行:“$json=$API->make_request$url,get;”通过“$json=$API->make_request$url,GET,?count=”$n、 "其中$n是您希望获得的推文数量。