Skip to content

Instantly share code, notes, and snippets.

@mpyw
Created October 11, 2012 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpyw/3871869 to your computer and use it in GitHub Desktop.
Save mpyw/3871869 to your computer and use it in GitHub Desktop.
Twitter Developers/Changing application's settings (PHP)
<?php
// PHP 移植バージョン。こっちはもっとデバッグしてないバージョンです。
// @re4k 2012/10/11
class TwitterDevelopers
{
private $cookie;
private $boundary = "re4kboundary";
private $user_agent = "Mozilla/5.0 (X11; Linux x86_64; rv:18.0) Gecko/18.0 Firefox/18.0 FirePHP/0.7.1";
private function request($uri, $type = "GET", $data)
{
$content = "";
$_c = [];
foreach($this->cookie as $k => $v)
{
$_c[] = "$k=$v";
}
$context = [
"http" => [
"header" => implode("\r\n", [
"Cookie: " . implode("; ", $_c),
"User-Agent: " . $this->user_agent
])
]
];
if($type == "POST_MULTIPART")
{
foreach($data as $key => $value)
{
$content .= "--$this->boundary\r\n";
$content .= "Content-Disposition: form-data; name=$key\r\n";
$content .= "\r\n";
$content .= $value;
$content .= "\r\n";
}
$content .= "--$this->boundary--\r\n";
$context["http"]["method"] = "POST";
$context["http"]["header"] = implode("\r\n", [
$context["http"]["header"],
"Content-Type: multipart/form-data; boundary=$this->boundary"
]);
$context["http"]["content"] = $content;
}
else
{
$content = http_build_query($data, "", "&");
$context["http"]["method"] = $type;
if($type == "POST")
{
$context["http"]["header"] = implode("\r\n", [
$context["http"]["header"],
"Content-Type: application/x-www-form-urlencoded"
]);
}
$context["http"]["content"] = $content;
}
$response = file_get_contents($uri, false, stream_context_create($context));
foreach($http_response_header as $r)
{
if(strpos($r, "Set-Cookie") === false)
{
continue;
}
list($k, $v) = explode("=", explode(";", explode(": ", $r)[1])[0]);
$this->cookie[$k] = $v;
}
return $response;
}
private function prepare_login()
{
$res = $this->request("https://dev.twitter.com/user/login", "GET", []);
$dom = new DOMDocument;
@$dom->loadHTML($res);
$xpath = new DOMXpath($dom);
return [
"form_build_id" => $xpath->query('//form[@id="user-login"]/input[@name="form_build_id"]')->item(0)->getAttributeNode("value")->value,
"form_token" => $xpath->query('//form[@id="user-login"]/input[@name="form_token"]')->item(0)->getAttributeNode("value")->value,
"form_id" => "user_login",
"op" => "Log in"
];
}
private function login($name, $pass)
{
$data = $this->prepare_login();
$data["name"] = $name;
$data["pass"] = $pass;
$res = $this->request("https://dev.twitter.com/user/login", "POST", $data);
}
private function prepare_update($id)
{
$res = $this->request("https://dev.twitter.com/apps/$id/settings", "GET", []);
$dom = new DOMDocument;
@$dom->loadHTML($res);
$xpath = new DOMXpath($dom);
return [
"name" => $xpath->query('//*[@id="edit-name"]')->item(0)->getAttributeNode("value")->value,
"description" => $xpath->query('//*[@id="edit-description"]')->item(0)->getAttributeNode("value")->value,
"url" => $xpath->query('//*[@id="edit-url"]')->item(0)->getAttributeNode("value")->value,
"files[image]" => "",
"access_level" => $xpath->query('//input[@name="access_level" and @checked="checked"]')->item(0)->getAttributeNode("value")->value,
"callback_url" => $xpath->query('//*[@id="edit-callback-url"]')->item(0)->getAttributeNode("value")->value,
"organization" => $xpath->query('//*[@id="edit-organization"]')->item(0)->getAttributeNode("value")->value,
"organization_url" => $xpath->query('//*[@id="edit-organization-url"]')->item(0)->getAttributeNode("value")->value,
"op" => "Update this Twitter application's settings",
"form_build_id" => $xpath->query('//form[@id="twitter-apps-form-settings"]/input[@name="form_build_id"]')->item(0)->getAttributeNode("value")->value,
"form_token" => $xpath->query('//form[@id="twitter-apps-form-settings"]/input[@name="form_token"]')->item(0)->getAttributeNode("value")->value,
"form_id" => "twitter_apps_form_settings"
];
}
public function update($id, $newname)
{
$data = $this->prepare_update($id);
$data["name"] = $newname;
$res = $this->request("https://dev.twitter.com/apps/$id/settings", "POST_MULTIPART", $data);
}
public function __construct($name, $pass)
{
$this->cookie = [];
$this->login($name, $pass);
}
}
$test = new TwitterDevelopers("screen_name", "password");
$test->update("app_id", "new_name");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment