Skip to content

Instantly share code, notes, and snippets.

@shobotch
Created February 4, 2012 03:25
Show Gist options
  • Save shobotch/1734939 to your computer and use it in GitHub Desktop.
Save shobotch/1734939 to your computer and use it in GitHub Desktop.
ktaiweb投稿クラス【完成したけどデバッグまだ】
<?php
/*****************************************************************************
このクラスは某、打ち止め氏の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("ツイート"); //投稿する!
*/
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;
function __construct($userName,$password){
$this->userName=$userName;
$this->password=$password;
$socket = fsockopen("twtr.jp",80);
$request = array(
'GET / HTTP/1.1',
'Host: twtr.jp',
'User-Agent: Mozilla/0 (iPhone;)',
"\r\n"
);
fwrite($socket, join($request,"\r\n"));
ob_start();
fpassthru($socket);
$res = ob_get_clean();
$res = explode("\r\n\r\n",$res,2);
//HTMLからauthenticity_tokenを取得
$token["dom"] = @DOMDocument::loadHTML($res[1]);
$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("/_komadori_session=.+?;/",$res[0],$matches);
$this->cookie = $matches[0];
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();
$this->a = $res;
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;
$res = explode("\r\n\r\n",$res,2);
preg_match("/_komadori_session=.+?;/",$res[0],$matches);
$this->cookie = $matches[0];
}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',
'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
));
if($response){
$request = array(
"POST /status/create HTTP/1.1",
'Host: twtr.jp',
'User-Agent: '.$this->userAgent,
'Cookie: '.$this->cookie,
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: '.strlen($post)
);
}else{
$request = array(
"POST /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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment