-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathniconico.class.php
74 lines (71 loc) · 2.08 KB
/
niconico.class.php
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
<?php
class niconico {
private $user_session;
function __construct($mail, $password){
$url = "https://secure.nicovideo.jp/secure/login?site=niconico"; // ログインURL
$data = array(
"next_url" => "",
"mail" => $mail,
"password" => $password,
"submit" => ""
);
$data = http_build_query($data);
$option = array(
"http" => array(
"method" => "POST",
"content" => $data
)
);
$context = stream_context_create($option);
$fp = @fopen($url, "rb", false, $context);
$response = @stream_get_meta_data($fp);
preg_match("(user_session=[a-z0-9_]+)",$response["wrapper_data"][8], $a);
if(count($a) == 0){
throw new Exception("ログインに失敗しました。");
}
$this->user_session = $a[0];
}
function getContext($type, $ContentType = "text/xml"){
return stream_context_create(
array(
"http"=> array(
"method" => strtoupper($type),
"header" => "Content-type: " . $ContentType . "\r\nCookie:".$this->user_session.";"
)
)
);
}
function getString($url){
$res = file_get_contents(
$url,
false,
$this->getContext("get")
);
return $res;
}
function getXML($url){
$res = $this->getString($url);
$xml = simplexml_load_string($res);
return $xml;
}
function getJSON($url){
$res = $this->getString($url);
$json = json_decode($res, true);
return $json;
}
function getJSONFromXML($url){
$res = $this->getString($url);
$xml = simplexml_load_string($res);
$json = json_encode($xml);
$json = json_decode($json, true);
return $json;
}
function postString($url){
$res = file_get_contents(
$url,
false,
$this->getContext("post")
);
return $res;
}
}