diff --git a/application/controllers/tweet_test.php b/application/controllers/tweet_test.php index 7fb1c5c..f04351d 100644 --- a/application/controllers/tweet_test.php +++ b/application/controllers/tweet_test.php @@ -77,4 +77,19 @@ function auth() var_dump($timeline); } + + // This is assuming that this is called from the form on the view and that + // the user has already been validated + function upload_img(){ + // Referenced from https://github.com/themattharris/tmhOAuth + $image = "@{$_FILES['image']['tmp_name']};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}"; + $data = array( + 'media[]' => $image, + 'status' => $this->input->post('status') + ); + + $result = $this->tweet->upload($data); + + var_dump($result); + } } \ No newline at end of file diff --git a/application/libraries/tweet.php b/application/libraries/tweet.php index 20504da..6fd7841 100755 --- a/application/libraries/tweet.php +++ b/application/libraries/tweet.php @@ -112,30 +112,41 @@ public function get($url, $params) return $response; } - public function post($url, $params) + public function post($url, $params, $multipart = false) { // Todo $post = ''; - foreach ( $params['request'] as $k => $v ) - { - $post .= "{$k}={$v}&"; + if(!$multipart){ + foreach ( $params['request'] as $k => $v ) + { + $post .= "{$k}={$v}&"; + } + + $post = substr($post, 0, -1); + } + else{ + $post = $params['request']; } - - $post = substr($post, 0, -1); $this->_initConnection($url, $params); curl_setopt($this->_ch, CURLOPT_POST, 1); curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $post); + // Debugging + $f = fopen('/Users/Lee/Desktop/request.txt', 'w'); + curl_setopt($this->_ch, CURLOPT_VERBOSE, true); + curl_setopt($this->_ch, CURLOPT_STDERR, $f); + curl_setopt($this->_ch, CURLOPT_SSL_VERIFYPEER, true); + $response = $this->_addCurl($url, $params); - return $response; + return $response; } private function _addOauthHeaders(&$ch, $url, $oauthHeaders) { - $_h = array('Expect:'); + $_h = array('Expect:', "Connection: Keep-Alive", "Cache-Control: no-cache", "User-Agent: " . $_SERVER['HTTP_USER_AGENT']); $urlParts = parse_url($url); $oauth = 'Authorization: OAuth realm="' . $urlParts['path'] . '",'; @@ -144,6 +155,7 @@ private function _addOauthHeaders(&$ch, $url, $oauthHeaders) $oauth .= "{$name}=\"{$value}\","; } + // Additional headrs $_h[] = substr($oauth, 0, -1); curl_setopt($ch, CURLOPT_HTTPHEADER, $_h); @@ -284,9 +296,11 @@ class tweetOauth extends tweetConnection { private $_version = '1.0'; private $_apiUrl = 'http://api.twitter.com'; private $_searchUrl = 'http://search.twitter.com/'; + private $_uploadUrl = 'https://upload.twitter.com/1/'; private $_callback = NULL; private $_errors = array(); private $_enable_debug = FALSE; + private $_multipart = FALSE; function __construct() { @@ -304,7 +318,7 @@ function __construct() 'access_key' => $this->_getAccessKey(), 'access_secret' => $this->_getAccessSecret() ); - + $this->_checkLogin(); } @@ -337,6 +351,13 @@ public function call($method, $path, $args = NULL) return ( $response === NULL ) ? FALSE : $response->_result; } + public function upload($args = NULL){ + $this->_multipart = TRUE; + $response = $this->_httpRequest('POST', $this->_uploadUrl.'statuses/update_with_media.json', $args); + + return ( $response === NULL ) ? FALSE : $response->_result; + } + public function search($args = NULL) { $response = $this->_httpRequest('GET', $this->_searchUrl.'search.json', $args); @@ -503,7 +524,7 @@ protected function _httpRequest($method = null, $url = null, $params = null) break; case 'POST': - return $this->_connection->post($url, $params); + return $this->_connection->post($url, $params, $this->_multipart); break; case 'PUT': @@ -551,6 +572,11 @@ private function _prepareParameters($method = NULL, $url = NULL, $params = NULL) array_walk($oauth, array($this, '_encode_rfc3986')); + if($this->_multipart){ + $request_params = $params; + $params = array(); + } + if ( is_array($params) ) { array_walk($params, array($this, '_encode_rfc3986')); @@ -561,7 +587,13 @@ private function _prepareParameters($method = NULL, $url = NULL, $params = NULL) ksort($encodedParams); $oauth['oauth_signature'] = $this->_encode_rfc3986($this->_generateSignature($method, $url, $encodedParams)); - return array('request' => $params, 'oauth' => $oauth); + + if($this->_multipart){ + return array('request' => $request_params, 'oauth' => $oauth); + } + else{ + return array('request' => $params, 'oauth' => $oauth); + } } private function _generateNonce() @@ -583,17 +615,27 @@ private function _generateSignature($method = null, $url = null, $params = null) foreach ($params as $k => $v) { + $k = $this->_encode_rfc3986($k); $v = $this->_encode_rfc3986($v); $concatenatedParams .= "{$k}={$v}&"; } $concatenatedParams = $this->_encode_rfc3986(substr($concatenatedParams, 0, -1)); - + // normalize url - $normalizedUrl = $this->_encode_rfc3986($this->_normalizeUrl($url)); + // Using the normalized url when uploading generates a 401 issue, yet for other api calls there's not a problem... + if($this->_multipart){ + $normalizedUrl = $this->_encode_rfc3986($url); + } + else{ + $normalizedUrl = $this->_encode_rfc3986($this->_normalizeUrl($url)); + } + $method = $this->_encode_rfc3986($method); // don't need this but why not? $signatureBaseString = "{$method}&{$normalizedUrl}&{$concatenatedParams}"; + + return $this->_signString($signatureBaseString); }