昼間の打ち合わせの時に話にでてきたTumblr。
そーいえば、登録していたことを思い出して、使い始めてみました。
てか、バイト先の上司に前に教えてもらったような・・・気もします。が、忘れましたw
で、まあ普通に登録して、Firefoxのプラグインとか、入れて使うのもいいし、WordPressのプラグイン使うもよいのですが、PHP使って、API叩いて投稿するPHP書いてみました。
普通にAPI叩くところは、いいとして、前回つくっていた、HTTPClientを改造して、POST出来るようにしてみました。
たしか前回はGETだけだったはず。
PEAR使えよって話ですが、やっぱそこは自分でかいてみないとwww
とりあえず、GETもPOSTもそんなに変わらないのですが、なぜかハマッてました・・。
FirefoxのLive HTTP headerつかって、POSTの内容とか確認してやってたのですが・・・。
とりあえず、POSTで送る内容を&で区切ってなかったので、apacheからエラーが帰ってきました。
あとは、POSTの値を表示する前(Content-Length)の後に\r\nだけしか入れてなかったとか・・・。
正しくは、\r\n\r\nでしたね・・・。結構恥ずかしい。。。
もっと直しますが、ソース上げておきます。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
//HTTPClient.php
<?php
class HTTPClient
{
public $host = "www.polidog.jp";
public $port = 80;
private $postData = array();
public function sendGet($path=”,$body=”){
$ret = null;
try{
//値チェック
if(empty($path)){throw new Exception("pathが存在しません");}
if(empty($this->host)){throw new Exception("ホスト名が存在しません");}
if(!is_numeric($this->port)){throw new Exception("ポート番号が不正です");}
$fp = fsockopen( $this->host, $this->port, $errno, $errstr, 30);
if(!$fp){throw new Exception("HTTP接続ができません");}
$http_header .= "GET $path HTTP/1.0\r\n";
$http_header .= "Host: ".$this->host."\r\n";
$http_header .= "Accept: */*\r\n";
$http_header .= "Connection: Close\r\n\r\n";
fwrite($fp,$http_header);
while ( !feof( $fp ) ) {
$ret .= fgets( $fp, 128 );
}
fclose( $fp );
}catch(Exception $e){
print $e->getMessage();
exit();
}
return $ret;
}
public function sendPost($path=null,$post= null){
try{
if(empty($path)){throw new Exception("pathが存在しません");}
if(empty($this->host)){throw new Exception("ホスト名が存在しません");}
if(!is_numeric($this->port)){throw new Exception("ポート番号が不正です");}
if (is_null($post)) {
$post = $this->postData;
}
foreach($post as $key => $value){
$send_data_array[] = urlencode($key)."=".urlencode($value);
}
$send_data = implode("&",$send_data_array);
$length = strlen($send_data);
$http_header .= "POST $path HTTP/1.0\r\n";
$http_header .= "Host: ".$this->host."\r\n";
$http_header .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
$http_header .= "Accept-Language: ja,en-us;q=0.7,en;q=0.3\r\n";
$http_header .= "Accept-Encoding: gzip,deflate\r\n";
$http_header .= "Accept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7\r\n";
$http_header .= "Keep-Alive: 300\r\n";
$http_header .= "Connection: keep-alive\r\n";
$http_header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$http_header .= "Content-Length: $length\r\n\r\n";
$http_header .= $send_data;
$fp = fsockopen( $this->host, $this->port, $errno, $errstr, 30);
fwrite($fp,$http_header);
while ( !feof( $fp ) ) {
$ret .= fgets( $fp, 128 );
}
fclose( $fp );
return $ret;
}catch(Exception $e){
print $e->getMessage();
exit();
}
}
function addPostData($key = null, $value = null){
if(is_null($key) || is_null($value)){
return false;
}
$this->postData[$key] = $value;
return true;
}
}
?>
|
んで、実際にこんな感じでPOSTすれば投稿できます。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php
require_once "HTTPClient.php";
$http = new HTTPClient();
$http->host = "www.tumblr.com";
$http->addPostData("email","[email protected]");
$http->addPostData("password","*******");
$http->addPostData("type","regular");
$http->addPostData("title","APIテスト");
$http->addPostData("body","ぼでぃぇ");
$ret = $http->sendPost("/api/write");
var_dump($ret);
?>
|
APIのマニュアルが英語しかなかったんですが・・・いちお
「regular」だと、普通にTextの投稿っぽいです。
まあ時間ができたらもう少し、楽しんでみます。