Skip to content

Instantly share code, notes, and snippets.

@shobotch
Created February 4, 2012 00:55
Show Gist options
  • Save shobotch/1734142 to your computer and use it in GitHub Desktop.
Save shobotch/1734142 to your computer and use it in GitHub Desktop.
ktaiweb投稿クラス【未完成】
/*****************************************************************************
このクラスは某、打ち止め氏のktaiwebから投稿するためのクラスを書きなおしたものです。
cURLの方がアクセスは何故か早かったので、authenticity_tokenとCookieはcURLで取得しています。
PHP.iniの設定でextension=php_curl.dllをコメントアウトしていると正しく使えず、errorが出る可能性があります。
;を外して再起動すれば使えるようになると思います。
-----テスト環境-----
・Windows Vista SP1
・Apache/2.2.17 (Win32)
・PHP5.3.4
・Firefox 10.0
尚、メソッドの名前は私が前から作成していた名前を使っていますので、本家のとは違うところがあります。ご確認ください。
このライブラリを使用して如何なる損益がありましても、私は一切責任を負いません。
使いすぎによるアカウント凍結にご注意を。
【重要】まだ書き込めないのでバグではなく仕様です。
******************************************************************************/
/*
* $post = new ktaiWeb("username","password");
* $post->tweet(rand()); //投稿する!
*/
class ktaiWeb{
private $userName; //twitterのuser name
private $password; //twitterのpassword
private $authenticity_token; //formのauthenticity_token
private $cookie;
private $login;
public $userAgent = "Mozilla/0 (iPhone;)";
private $curl;
private $socket;
function __construct($userName,$password){
$this->userName=$userName;
$this->password=$password;
$this->curl=curl_init();
curl_setopt($this->curl, CURLOPT_URL, "http://twtr.jp/");
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($this->curl, CURLOPT_USERAGENT, $this->userAgent);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($this->curl, CURLOPT_HEADER, 1);
$output = curl_exec($this->curl); //curl実行
//HTMLからauthenticity_tokenを取得
$token["dom"] = @DOMDocument::loadHTML($output);
$token["xml"] = simplexml_import_dom($token["dom"]);
$token["token"] = $token["xml"]->xpath('/html/body/div[3]/form/div/input');
$this->authenticity_token = (string)$token["token"][0]["value"];
//HTMLからSet-Cookieを正規表現で取得
preg_match("/^Set-Cookie: ([^;]+);/m",$output,$matches);
$this->cookie = $matches[1];
if(!empty($this->authenticity_token) && !empty($this->cookie)){
//ログイン処理
$socket = fsockopen("twtr.jp",80);
$post = http_build_query(
array(
'login'=>$this->userName,
'password'=>$this->password,
'authenticity_token'=>$this->authenticity_token
),"","&");
//出力例: login=userName&password=password&authenticity_token=authenticity_token
$request = array(
'POST /login/ HTTP/1.1',
'Host: twtr.jp',
'User-Agent: '.$this->userAgent,
'Cookie:'.$this->cookie,
'Content-type: application/x-www-form-urlencoded',
'Content-length: '.strlen($post)
);
fwrite($socket, implode($request,"\r\n")."\r\n\r\n".$post);
ob_start();
fpassthru($socket);
$res = ob_get_clean();
fclose($socket);
preg_match("/^Location: http:\/\/twtr.jp\/(.+)\r\n/m",$res,$matches);
/*
* ログインできたかどうか確認
* ---失敗時---
* Array ( [0] => Location: http://twtr.jp/login?login=username [1] => login?login=username )
* ---成功時---
* Array ( [0] => Location: http://twtr.jp/home [1] => home )
*/
if($matches[1]==="home"){
$this->login = true;
}else{
$this->login = false;
}
}
}
function getUserStatus($userName){
if(!$this->login)return false;
$socket = fsockopen("twtr.jp",80);
$request = array(
'GET /user/'.$userName.'/status HTTP/1.1',
'Expect:',
'Host: twtr.jp',
'Connection: keep-alive',
'User-Agent: '.$this->userAgent,
'Referer: http://twtr.jp/',
'Cookie:'.$this->cookie,
'Content-type: text/html; charset=utf-8',
);
fwrite($socket, implode($request,"\r\n")."\r\n\r\n");
ob_start();
fpassthru($socket);
$res = ob_get_clean();
$res = explode("\r\n\r\n",$res,2);
fclose($socket);
return $res[1];
}
function getTL(){
if(!$this->login)return false;
$socket = fsockopen("twtr.jp",80);
$request = array(
'GET /home HTTP/1.1',
'Host: twtr.jp',
'Connection: keep-alive',
'User-Agent: '.$this->userAgent,
'Referer: http://twtr.jp/',
'Cookie:'.$this->cookie,
'Content-type: text/html; charset=utf-8',
);
fwrite($socket, implode($request,"\r\n")."\r\n\r\n");
ob_start();
fpassthru($socket);
$res = ob_get_clean();
$res = explode("\r\n\r\n",$res,2);
fclose($socket);
return $res[1];
}
function tweet($tweet,$in_reply_to_status_id="",$response=TRUE){
if(!$this->login)return false;
$socket = fsockopen("twtr.jp",80);
$post = http_build_query(array(
"text"=>$tweet,
"in_reply_to" =>$in_reply_to_status_id,
"authenticity_token"=>$this->authenticity_token
));
$request = array(
"GET /status/create HTTP/1.1",
'Host: twtr.jp',
'User-Agent: '.$this->userAgent,
'Connection: Close', //こいつを追加
'Cookie:'.$this->cookie.";",
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: '.strlen($post)
);
fwrite($socket, implode($request,"\r\n")."\r\n\r\n".$post);
ob_start();
fpassthru($socket);
$res = ob_get_clean();
fclose($socket);
return $res;
}
function test(){
return array(
$this->userName,
$this->password,
$this->authenticity_token,
$this->cookie
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment