update_with_media media[] param format


#1

I have a question about the media[] param for the update_with_media endpoint. This code works perfectly, using this library https://github.com/abraham/twitteroauth

     $oauth_token='******************';
     $oauth_token_secret='******************';
     $consumer_key='******************';
     $consumer_secret='******************';

	$tmhOAuth = new tmhOAuth(array(
		'consumer_key'    => $consumer_key,
		'consumer_secret' => $consumer_secret,
		'user_token'      => $oauth_token,
		'user_secret'     => $oauth_token_secret,
	));

	$image = "{$_FILES['photoimg']['tmp_name']};type={$_FILES['photoimg']['type']};filename={$_FILES['photoimg']['name']}";

	$code = $tmhOAuth->request('POST', 'https://api.twitter.com/1.1/statuses/update_with_media.json',
						array(
						     'media[]'  => "@{$image}",
						     'status'   => " " . $status //A space is needed because twitter b0rks if first char is an @
						 ),
						true, // use auth
						true  // multipart
						);

	if ($code == 200) {

		$content = "<p>Upload success. Image posted to Twitter.</p>";		

	} else {

		$content = "Damn! Something went wrong. Sorry :-("  
		."<br /> code=" . $code
		."<br /> status=" . $status
		."<br /> image=" . $image
		."<br /> response=<pre>"
		. print_r($tmhOAuth->response['response'], TRUE)
		. "</pre><br /> info=<pre>"
		. print_r($tmhOAuth->response['info'], TRUE)
		. "</pre><br /> code=<pre>"
		. print_r($tmhOAuth->response['code'], TRUE) . "</pre>";
	}
		
     echo  $content;

However, the image I want to upload is not coming from a form, it is coming from my database. What is the proper format for the media[] attribute if I want to upload a photo via a link, such as http://mysite.com/image/img.png, or is there another way to properly do this? I’ve tried just the link in the code above and have gotten an error. I also tried

$image=@{http://mysite.com/image/img.png};type={image/png};filename={http://mysite.com/image/img.png}

and variations, and have gotten 403 errors. Anyone know the right way?


#2

Found a solution on the following link, I used the function at the bottom to mimic a form, populated $_FILES


#3

Your media field format is incorrect. It should be binary string of image file content.
See sample here -