Skip to content
This repository was archived by the owner on Jan 10, 2020. It is now read-only.

Commit

Permalink
Adding postMultipartFormData() method
Browse files Browse the repository at this point in the history
  • Loading branch information
cakebaker committed Jun 15, 2012
1 parent 1c7b8b1 commit 81b5395
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions Vendor/OAuth/OAuthClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct($consumerKey, $consumerSecret = '') {
}

/**
* Call API with a GET request. Returns either false on failure or the response body.
* Call API with a GET request. Returns either false on failure or an HttpResponse object.
*/
public function get($accessTokenKey, $accessTokenSecret, $url, array $getData = array()) {
$accessToken = new OAuthToken($accessTokenKey, $accessTokenSecret);
Expand All @@ -45,7 +45,7 @@ public function getAccessToken($accessTokenURL, $requestToken, $httpMethod = 'PO
}

/**
* Returns an array with the complete response of the previous request, or null, if there was no request.
* Returns an HttpResponse object for the previous request, or null, if there was no request.
*/
public function getFullResponse() {
return $this->fullResponse;
Expand All @@ -69,7 +69,7 @@ public function getRequestToken($requestTokenURL, $callback = 'oob', $httpMethod
}

/**
* Call API with a POST request. Returns either false on failure or the response body.
* Call API with a POST request. Returns either false on failure or an HttpResponse object.
*/
public function post($accessTokenKey, $accessTokenSecret, $url, array $postData = array()) {
$accessToken = new OAuthToken($accessTokenKey, $accessTokenSecret);
Expand All @@ -78,6 +78,20 @@ public function post($accessTokenKey, $accessTokenSecret, $url, array $postData
return $this->doPost($url, $request->to_postdata());
}

/**
* Call API with a POST request, the content type set to multipart/form-data.
* This is, for example, necessary for Twitter's update_with_media API method (https://dev.twitter.com/docs/api/1/post/statuses/update_with_media)
* $paths a key-value array, example: array('media[]' => '/home/dho/avatar.png')
* Returns either false on failure or an HttpResponse object.
*/
public function postMultipartFormData($accessTokenKey, $accessTokenSecret, $url, array $paths, array $postData = array()) {
$accessToken = new OAuthToken($accessTokenKey, $accessTokenSecret);
$request = $this->createRequest('POST', $url, $accessToken, array());
$authorization = str_replace('Authorization: ', '', $request->to_header());

return $this->doPostMultipartFormData($url, $authorization, $paths, $postData);
}

protected function createOAuthToken(array $response) {
if (isset($response['oauth_token']) && isset($response['oauth_token_secret'])) {
return new OAuthToken($response['oauth_token'], $response['oauth_token_secret']);
Expand Down Expand Up @@ -114,6 +128,38 @@ private function doPost($url, $data) {
return $result;
}

private function doPostMultipartFormData($url, $authorization, $paths, $data) {
App::uses('String', 'Utility');
$boundary = String::uuid();

$body = "--{$boundary}\r\n";

foreach ($data as $key => $value) {
$body .= "Content-Disposition: form-data; name=\"{$key}\"\r\n";
$body .= "\r\n";
$body .= "{$value}\r\n";
$body .= "--{$boundary}\r\n";
}

foreach ($paths as $key => $path) {
$body .= "Content-Disposition: form-data; name=\"{$key}\"; filename=\"{$path}\"\r\n";
$body .= "\r\n";
$body .= file_get_contents($path) . "\r\n";
$body .= "--{$boundary}--\r\n";
}

$socket = new HttpSocket();
$result = $socket->request(array('method' => 'POST',
'uri' => $url,
'header' => array(
'Authorization' => $authorization,
'Content-Type' => "multipart/form-data; boundary={$boundary}"),
'body' => $body));
$this->fullResponse = $socket->response;

return $result;
}

private function doRequest($request) {
if ($request->get_normalized_http_method() == 'POST') {
$data = $this->doPost($this->url, $request->to_postdata());
Expand Down

0 comments on commit 81b5395

Please sign in to comment.