Twitter api kullanarak daha basit bir şekilde tweet atabilirsin, fakat kullanıcı adı ve şifreyle giriş yaparakda aşağıdaki daha önce hazırladıgım sınıfı kullanabilirsin.
<?php
class twitter{
public $username;
public $password;
public $tweet;
public $cookie_file = '/';
private $connect_url;
private $post_field = array();
private $content_type;
private function setPostField($post_field)
{
$this->post_field = $post_field;
}
private function curl()
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->connect_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_COOKIE, true);
curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie_file);
curl_setopt($curl, CURLOPT_REFERER, 'https://mobile.twitter.com/session/new');
curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookie_file);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
if($this->post_field){
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->post_field);
}
$result = curl_exec($curl);
return $result;
}
private function page_token($source)
{
preg_match('#<input type="hidden" value="(.*?)"#si', $source, $page_token);
if(isset($page_token[1]) && !empty($page_token[1])){
return $page_token[1];
}else{
$this->error[] = 'Sayfa token bilgisi alınamadı';
return false;
}
}
private function login()
{
$this->connect_url = 'http://www.twitter.com';
$source_main = $this->curl();
if(!$source_main){
$this->error[] = 'Anasayfaya bağlanamadı';
return false;
}
preg_match('#<b class="fullname">(.*?)</b>#si', $source_main, $logged);
if(!isset($logged[1])){
preg_match('#token" value="(.*?)"#si', $source_main, $token);
if(!isset($token[1]) || empty($token[1])){
$this->error[] = 'Token Bilgisi alınamadı';
return false;
}
$this->connect_url = 'https://twitter.com/sessions';
$this->setPostField('session%5Busername_or_email%5D='.$this->username.'&session%5Bpassword%5D='.$this->password.'&return_to_ssl=true&scribe_log=&redirect_after_login=%2F&authenticity_token='.$token[1]);
$r = $this->curl();
preg_match('#<b class="fullname">(.*?)</b>#si', $r, $logged);
if(isset($logged[1])){
return $this->page_token($source_main);
}else{
$this->error[] = 'Giriş Yapılamadı';
return false;
}
}else{
return $this->page_token($source_main);
}
}
private function tweet()
{
$logged = $this->login();
if(!$logged){
return false;
}
$this->connect_url = 'https://twitter.com/i/tweet/create';
$this->setPostField('authenticity_token='.$logged.'&place_id=&status='.$this->tweet);
$post = json_decode($this->curl());
if(is_object($post)){
return $post->message;
}else{
$this->error[] = 'Gelen mesaj alınamadı';
return false;
}
}
public function render()
{
$q = $this->tweet();
if($q){
return $q;
}
return false;
}
}
$twitter = new twitter();
$twitter->username = 'denemekullaniciadi'; // kullanıcı adı
$twitter->password = 'denemesifre'; // Şifre
$twitter->tweet = 'hey gidi günler :Ğ'; // atılacak tweet
$twitter->cookie_file = dirname(__FILE__).'/'.$twitter->username.'_cookie.txt'; // çerez dosya adı
$q = $twitter->render();
if($q){
echo $q;
}else{
echo '<pre>';
print_r($twitter->error);
echo '</pre>';
}