function __construct() { App::import(array('type' => 'File', 'name' => 'Twitter.TWITTER_CONFIG', 'file' => 'config' . DS . 'core.php')); App::import(array('type' => 'File', 'name' => 'Twitter.TwitterSource', 'file' => 'models' . DS . 'datasources' . DS . 'twitter_source.php')); $TwitterConfig =& new TWITTER_CONFIG(); ConnectionManager::create('twitter', $TwitterConfig->login); $this->Api = ConnectionManager::getDataSource('twitter'); #debug($config); parent::__construct(); }
/** * Deletes a tweet * * @param integer $id Id of the tweet to be deleted * @param boolean $cascade * @return boolean */ public function delete($id = null, $cascade = true) { $this->request = array('uri' => array('path' => '1/statuses/destroy', 'query' => array('id' => $id)), 'auth' => true); return parent::delete($id, $cascade); }
/** * Upload an image and optional message to Twitpic * ----------------------------------------------- * * Twitpic::save(array( * 'Twitpic' => array( * 'message' => 'Optional message', * 'media' => array( * 'tmp_name' => '/path/to/file', * 'type' => 'image/jpeg', // mimetype * 'name' => 'filename.jpg', // * ) * ) * )); * * **Notes:** * * - The media key in the $data array is the same (or a subset of) the data * you get when you upload a file through an html form. * - There are validation rules for extension to be an image and message * length < 140 chars * - See here for more details http://dev.twitpic.com/docs/2/upload/ * * @param mixed $data * @param mixed $validate * @param mixed $fieldList * @return mixed */ public function save($data = null, $validate = true, $fieldList = array()) { $ds = $this->getDataSource(); // Get the authorization header that twitpic will pass on to twitter to // verify the credentials of the request. $authorizationHeader = $ds->Http->authorizationHeader(array('uri' => array('scheme' => 'https', 'host' => 'api.twitter.com', 'path' => '1/account/verify_credentials.json'), 'method' => 'GET', 'auth' => array('realm' => 'http://api.twitter.com/', 'oauth_consumer_key' => $ds->config['oauth_consumer_key'], 'oauth_consumer_secret' => $ds->config['oauth_consumer_secret'], 'oauth_token' => $ds->config['oauth_token'], 'oauth_token_secret' => $ds->config['oauth_token_secret']))); // The boundary string is used to identify the different parts of a // multipart http request $boundaryString = 'Next_Part_' . String::uuid(); // Build the multipart body of the http request, the first part is the api // key of your application whihc you get from Twitpic $body = "--{$boundaryString}\r\n"; $body .= "Content-Disposition: form-data; name=\"key\"\r\n"; $body .= "\r\n"; $body .= $ds->config['twitpic_api_key'] . "\r\n"; // If there is a message, add this to the next part of the request body. if (!empty($data[$this->alias]['message'])) { $body .= "--{$boundaryString}\r\n"; $body .= "Content-Disposition: form-data; name=\"message\"\r\n"; $body .= "\r\n"; $body .= $data[$this->alias]['message'] . "\r\n"; } // Finally add the headers and binary data for the image $body .= "--{$boundaryString}\r\n"; $body .= "Content-Disposition: form-data; name=\"media\"; filename=\"{$data[$this->alias]['media']['name']}\"\r\n"; $body .= "Content-Type: {$data[$this->alias]['media']['type']}\r\n"; $body .= "\r\n"; $body .= file_get_contents($data[$this->alias]['media']['tmp_name']) . "\r\n"; $body .= "--{$boundaryString}--\r\n"; // Tie it all together in the model's request property $this->request = array('uri' => array('path' => '2/upload', 'host' => 'api.twitpic.com'), 'header' => array('Content-Type' => 'multipart/form-data; boundary="' . $boundaryString . '"', 'X-Auth-Service-Provider' => 'https://api.twitter.com/1/account/verify_credentials.json', 'X-Verify-Credentials-Authorization' => $authorizationHeader), 'body' => $body); $result = parent::save($data, $validate, $fieldList); if ($result && !empty($this->response['id'])) { $this->setInsertID($this->response['id']); } return $result; }