diff --git a/DiasporaAPI.php b/DiasporaAPI.php new file mode 100644 index 0000000..ee5029f --- /dev/null +++ b/DiasporaAPI.php @@ -0,0 +1,78 @@ +tmp_cookie_file); + curl_setopt($ch, CURLOPT_COOKIEJAR, $this->tmp_cookie_file); + $http_status = curl_getinfo($http, CURLINFO_HTTP_CODE); + $content = curl_exec($ch); + curl_close($ch); + + return array('status_code' => $http_status, 'text' => $content); + } + + public function __construct($pod, $username, $password) { + $this->pod = $pod; + $data = array( + 'user[username]' => $username, + 'user[password]' => $password, + 'authenticity_token' => $this->_fetch_token() + ); + $response = $this->_curl($this->pod."/users/sign_in", "post", $data); + if ($response['status_code'] != 302) { + throw new Exception("Invalid status code: ".$response['status_code']); + } + $this->tmp_cookie_file = tempnam(sys_get_temp_dir(), 'cookiejar'); + } + + public function __destruct() { + unlink($this->tmp_cookie_file); + } + + private function _fetch_token() { + $response = $this->_curl($this->pod."/stream"); + $matches = array(); + preg_match(self::$_token_regex, $response['text'], $matches); + return $matches[1]; + } + + public function post($text, $aspect_ids='public') { + $data = json_encode(array( + 'status_message' => array('text' => $text), + 'aspect_ids' => $aspect_ids + )); + $headers = array( + 'content-type' => 'application/json', + 'accept' => 'application/json', + 'x-csrf-token' => $this->_fetch_token() + ); + $response = $this->_curl($this->pod."/status_messages", "post", $data, $headers); + if ($response['status_code'] != 201) { + throw new Exception("Invalid status code: ".$response['status_code']); + } + } + + } + + }