|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Twitter.class.php - update the status of a twitter user |
|
5
|
|
|
* Author: Felix Oghina |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
if (class_exists('Twitter')) { |
|
9
|
|
|
return true; |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
|
class Twitter |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
private $auth = false; |
|
16
|
|
|
private $debug = false; |
|
17
|
|
|
public $error = false; |
|
18
|
|
|
|
|
19
|
|
|
function __construct($user, $pass, $debug = false) |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
|
|
// Store an auth key for the HTTP Authorization: header |
|
22
|
|
|
$this->auth = base64_encode($user . ':' . $pass); |
|
|
|
|
|
|
23
|
|
|
$this->debug = $debug; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
function update($new_status) |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
if (strlen($new_status) > 140) { |
|
29
|
|
|
$this->error = "Status too long: {$new_status}."; |
|
30
|
|
|
return false; |
|
31
|
|
|
} |
|
32
|
|
|
$fp = @fsockopen('twitter.com', 80, $errno, $errstr); |
|
33
|
|
|
if (!$fp) { |
|
34
|
|
|
$this->error = "Socket error #{$errno}: {$errstr}"; |
|
35
|
|
|
return false; |
|
36
|
|
|
} |
|
37
|
|
|
$post_data = 'status=' . urlencode($new_status); |
|
38
|
|
|
$to_send = "POST /statuses/update.xml HTTP/1.1\r\n"; |
|
39
|
|
|
$to_send .= "Host: twitter.com\r\n"; |
|
40
|
|
|
$to_send .= 'Content-Length: ' . strlen($post_data) . "\r\n"; |
|
41
|
|
|
$to_send .= "Authorization: Basic {$this->auth}\r\n\r\n"; |
|
42
|
|
|
$to_send .= $post_data . "\r\n\r\n"; |
|
43
|
|
|
$bytes = fwrite($fp, $to_send); |
|
44
|
|
|
if ($bytes === false) { |
|
45
|
|
|
$this->error = 'Socket error: Error sending data.'; |
|
|
|
|
|
|
46
|
|
|
return false; |
|
47
|
|
|
} elseif ($bytes < strlen($to_send)) { |
|
48
|
|
|
$this->error = 'Socket error: Could not send all data.'; |
|
|
|
|
|
|
49
|
|
|
return false; |
|
50
|
|
|
} |
|
51
|
|
|
if ($this->debug) echo "Sent:\n{$to_send}\n\n"; |
|
52
|
|
|
$response = ''; |
|
53
|
|
|
while (!feof($fp)) { |
|
54
|
|
|
$buf = fread($fp, 1024); |
|
55
|
|
|
if ($buf === false) { |
|
56
|
|
|
$this->error = 'Socket error: Error reading data.'; |
|
|
|
|
|
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
$response .= $buf; |
|
60
|
|
|
} |
|
61
|
|
|
if ($this->debug) echo "Received:\n{$response}"; |
|
62
|
|
|
$was_error = preg_match('#' . preg_quote('<error>') . '(.+)' . preg_quote('</error>') . '#i', $response, $matches); |
|
63
|
|
|
if ($was_error) { |
|
64
|
|
|
$this->error = "Twitter error: {$matches[1]}"; |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
list($first_line) = explode("\r\n", $response); |
|
68
|
|
|
if ($first_line !== 'HTTP/1.1 200 OK') { |
|
69
|
|
|
$this->error = "Request error: {$first_line}"; |
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.