Skip to content

Commit

Permalink
Fix: update_with_media API call
Browse files Browse the repository at this point in the history
  • Loading branch information
yegle committed Dec 23, 2013
1 parent 73aebd3 commit 74412a2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
34 changes: 34 additions & 0 deletions filters/1_1__statuses__update_with_media_json.php
@@ -0,0 +1,34 @@
<?php

$filterName = basename(__FILE__, '.php');

$this->filters[$filterName] = function($args) {
$url = sprintf("https://api.twitter.com/%s", $args['path']);
$headers = OAuthUtil::get_headers();
// Check actually media uplaod
if(strpos(@$headers['Content-Type'], 'multipart/form-data') === FALSE
or count($_FILES) == 0 or !isset($_FILES['media'])) {
header('HTTP/1.0 400 Bad Request');
return;
}

$auth_headers = $args['self']->connection->getOAuthRequest(
$url, $args['method'], null)->to_header();
$forwarded_headers = array(
"Host: api.twitter.com",
$auth_headers,
"Expect:");
$parameters = preg_replace('/^@/', "\0@", $_POST);

$media = $_FILES['media'];
$fn = is_array($media['tmp_name']) ? $media['tmp_name'][0] : $media['tmp_name'];
$parameters["media[]"] = '@' . $fn;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $forwarded_headers);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($args['self'],'headerfunction'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$ret = curl_exec($ch);
return $ret;
};
32 changes: 0 additions & 32 deletions twip.php
Expand Up @@ -196,38 +196,6 @@ private function override_mode($imageproxy = FALSE){
));
return;


// Process with update_with_media
if($this->method === 'POST' && strpos($this->request_uri,'statuses/update_with_media') !== FALSE) {
$this->request_headers = OAuthUtil::get_headers();

// Check actually media uplaod
if(strpos(@$this->request_headers['Content-Type'], 'multipart/form-data') !== FALSE
&& count($_FILES) > 0 && isset($_FILES['media'])) {

$header_authorization = $this->connection->getOAuthRequest($this->request_uri, $this->method, null)->to_header();
$this->forwarded_headers = array("Host: api.twitter.com", $header_authorization, "Expect:");
$this->parameters = preg_replace('/^@/', "\0@", $_POST);

$media = $_FILES['media'];
$fn = is_array($media['tmp_name']) ? $media['tmp_name'][0] : $media['tmp_name'];
$this->parameters["media[]"] = '@' . $fn;

$ch = curl_init($this->request_uri);
curl_setopt($ch,CURLOPT_HTTPHEADER,$this->forwarded_headers);
curl_setopt($ch,CURLOPT_HEADERFUNCTION,array($this,'headerfunction'));
curl_setopt($ch,CURLOPT_POSTFIELDS,$this->parameters);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
$ret = curl_exec($ch);

echo $ret;
return;
} else {
header('HTTP/1.0 400 Bad Request');
return;
}
}

// Add include_entities arg if not exists and API is configured to expand t.co
if($this->o_mode_parse_entities && !isset($_REQUEST['include_entities'])){
if(preg_match('/^[^?]+\?/', $this->request_uri)){
Expand Down

0 comments on commit 74412a2

Please sign in to comment.