From 2d3ceed455f35fdd1cfb23fced2be69d569c92e3 Mon Sep 17 00:00:00 2001 From: Fabien DUVAL Date: Mon, 16 Dec 2013 16:20:09 +0100 Subject: [PATCH] Minor modification to support the multipart post requests. It can now handle 'update_with_media' requests --- application/libraries/Twitteroauth.php | 35 ++++++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/application/libraries/Twitteroauth.php b/application/libraries/Twitteroauth.php index 601b8f9..a98735d 100755 --- a/application/libraries/Twitteroauth.php +++ b/application/libraries/Twitteroauth.php @@ -152,8 +152,8 @@ function get($url, $parameters = array()) { /** * POST wrapper for oAuthRequest. */ - function post($url, $parameters = array()) { - $response = $this->oAuthRequest($url, 'POST', $parameters); + function post($url, $parameters = array(), $multipart = false) { + $response = $this->oAuthRequest($url, 'POST', $parameters, $multipart); if ($this->format === 'json' && $this->decode_json) { return json_decode($response); } @@ -174,17 +174,32 @@ function delete($url, $parameters = array()) { /** * Format and sign an OAuth / API request */ - function oAuthRequest($url, $method, $parameters) { - if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) { + function oAuthRequest($url, $method, $parameters, $multipart = false) { + if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) { $url = "{$this->host}{$url}.{$this->format}"; } - $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters); + + $signature_parameters = array(); + if ($multipart) { + // When making a multipart request, use only oauth_* -keys for signature + foreach ($parameters AS $key => $value) { + if (strpos($key, 'oauth_') !== 0) { + continue; + } + $signature_parameters[$key] = $value; + } + $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $signature_parameters); + } + else { + $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters); + } + $request->sign_request($this->sha1_method, $this->consumer, $this->token); switch ($method) { case 'GET': return $this->http($request->to_url(), 'GET'); default: - return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata()); + return $this->http($request->get_normalized_http_url(), $method, ($multipart ? $parameters : $request->to_postdata()), $request, $multipart); } } @@ -193,7 +208,7 @@ function oAuthRequest($url, $method, $parameters) { * * @return API results */ - function http($url, $method, $postfields = NULL) { + function http($url, $method, $postfields = NULL, OAuthRequest $request = NULL, $multipart = false) { $this->http_info = array(); $ci = curl_init(); /* Curl settings */ @@ -201,7 +216,11 @@ function http($url, $method, $postfields = NULL) { curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout); curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout); curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE); - curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:')); + $headers = array('Expect:'); + if ($multipart) { + $headers[] = $request->to_header(); + } + curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer); curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader')); curl_setopt($ci, CURLOPT_HEADER, FALSE);