/** * Called by tweet or retweet * * @param mixed $data * @param mixed $validate * @param mixed $fieldList * @return mixed */ public function save($data = null, $validate = true, $fieldList = array()) { $this->request['auth'] = true; $result = parent::save($data, $validate, $fieldList); if ($result && !empty($this->response['id'])) { $this->setInsertID($this->response['id']); } return $result; }
/** * 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; }