3 * Ein fies zusammengehackter PHP-Diaspory-Client, der direkt von diesem abgeschaut ist:
4 * https://github.com/Javafant/diaspy/blob/master/client.py
10 function __construct($pod) {
11 $this->token_regex = '/content="(.*?)" name="csrf-token/';
14 $this->cookiejar = tempnam(sys_get_temp_dir(), 'cookies');
17 function __destruct() {
18 if (file_exists($this->cookiejar))
19 unlink($this->cookiejar);
22 function _fetch_token() {
25 curl_setopt ($ch, CURLOPT_URL, $this->pod . "/stream");
26 curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookiejar);
27 curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookiejar);
28 curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
29 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
31 $output = curl_exec ($ch);
34 // Token holen und zurückgeben
35 preg_match($this->token_regex, $output, $matches);
39 function login($username, $password) {
41 'user[username]' => $username,
42 'user[password]' => $password,
43 'authenticity_token' => $this->_fetch_token()
46 $poststr = http_build_query($datatopost);
48 // Adresse per cURL abrufen
51 curl_setopt ($ch, CURLOPT_URL, $this->pod . "/users/sign_in");
52 curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookiejar);
53 curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookiejar);
54 curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, false);
55 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
56 curl_setopt ($ch, CURLOPT_POST, true);
57 curl_setopt ($ch, CURLOPT_POSTFIELDS, $poststr);
60 $info = curl_getinfo($ch);
63 if($info['http_code'] != 302) {
64 throw new Exception('Login error '.print_r($info, true));
67 // Das Objekt zurückgeben, damit man Aurufe verketten kann.
71 function post($text, $provider = "diasphp") {
72 // post-daten vorbereiten
73 $datatopost = json_encode(array(
74 'aspect_ids' => 'public',
75 'status_message' => array('text' => $text,
76 'provider_display_name' => $provider)
81 'Content-Type: application/json',
82 'accept: application/json',
83 'x-csrf-token: '.$this->_fetch_token()
86 // Adresse per cURL abrufen
89 curl_setopt ($ch, CURLOPT_URL, $this->pod . "/status_messages");
90 curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookiejar);
91 curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookiejar);
92 curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, false);
93 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
94 curl_setopt ($ch, CURLOPT_POST, true);
95 curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
96 curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
99 $info = curl_getinfo($ch);
102 if($info['http_code'] != 201) {
103 throw new Exception('Post error '.print_r($info, true));
106 // Ende der möglichen Kette, gib mal "true" zurück.