This repository was archived by the owner on Feb 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTwitter.php
More file actions
116 lines (105 loc) · 3.12 KB
/
Twitter.php
File metadata and controls
116 lines (105 loc) · 3.12 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
class Twitter {
private $user;
private $password;
protected $_curl;
public function setCredentials($user,$password){
$this->user = $user;
$this->password = $password;
}
public function verifyCredentials($format = "xml"){
$verified = $this->_call("http://twitter.com/account/verify_credentials.".$format);
if(!$verified){
return false;
}
return $verified;
}
public function update($status,$format = "xml",$extra = false){
$data = array();
if($extra){
$fields = array('lat','long','in_reply_to_status_id');
foreach($fields as $field){
if(isset($extra[$field])){
$data[$field] = $extra[$field];
}
}
}
$data = array_merge($data,array("status" => $status));
if(!$this->_call("http://twitter.com/statuses/update.".$format,"POST",$data)){
return false;
}
return true;
}
public function updateProfile($profiledata,$format = "xml"){
if(!$this->_call("http://twitter.com/account/update_profile.".$format,"POST",$profiledata)){
return false;
}
return true;
}
public function directMessageNew($message,$recipient,$format = "xml"){
if(!$this->_call("http://twitter.com/direct_messages/new.".$format,"POST",array("screen_name" => $recipient,"text" => $message))){
return false;
}
return true;
}
public function getMentions($since_id = 0){
$params = array('count' => 200);
if ($since_id) {
$params['since_id'] = $since_id;
}
if ($result = $this->_call('http://twitter.com/statuses/mentions.xml', 'GET', $params)) {
$result = simplexml_load_string($result);
$mentions = array();
foreach ($result->status as $mention) {
$mentions[] = $mention;
}
return $mentions;
}
return false;
}
protected function _call($url,$action = "GET",$params = null){
$response = $this->_callDetailed($url,$action,$params);
if(!is_string($response)){
return false;
}
return $response;
}
protected function _initCurl()
{
if (!$this->_curl !== null) {
@curl_close($this->_curl);
}
$this->_curl = curl_init();
curl_setopt($this->_curl, CURLOPT_USERPWD, $this->user . ':' . $this->password);
curl_setopt($this->_curl, CURLOPT_HEADER, 0);
curl_setopt($this->_curl, CURLOPT_USERAGENT, 'lulzkaffee bot');
curl_setopt($this->_curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->_curl, CURLOPT_TIMEOUT, 10);
}
protected function _callDetailed($url,$action = "GET",$params = null){
if(!$this->user || !$this->password){
return false;
}
$this->_initCurl();
$query = array();
foreach ($params as $key => $value) {
if (trim($value)) {
$query[] = $key . '=' . urlencode(trim($value));
}
}
if ($action === 'POST') {
curl_setopt($this->_curl, CURLOPT_POST, true);
curl_setopt($this->_curl, CURLOPT_POSTFIELDS, implode('&', $query));
} elseif ($action === 'GET') {
$url .= '?' . implode('&', $query);
}
curl_setopt($this->_curl, CURLOPT_URL, $url);
$result = curl_exec($this->_curl);
$info = curl_getinfo($this->_curl);
if ($result && $info['http_code'] == 200) {
return $result;
}
return false;
}
}
?>