Im trying to post an image and I keep on getting error creating status.
This is the function.
I basically set the ur to the https upload.twitter.com.
Then I do some validation on the status message itself.
Then I take a url image. I use a curl function to follow any redirects.
Using file get contents I write that image to a local file. Then I echo some
stuff make sure that process worked. It does. Write bytes has a number
greater than zero and file exists always returns 1.
I set the file of data['media'] to what I think it is supposed to be
and I get an error everytime. Any clues?
-
public function statusUpdateWithMedia($status, $url)
-
{ -
$this->_init();
-
$this->setUri('https://upload.twitter.com');
-
$path = '/1/statuses/update_with_media.xml';
-
$len = iconv_strlen(htmlspecialchars($status, ENT_QUOTES, 'UTF-8'), 'UTF-8');
-
if ($len > self::STATUS_MAX_CHARACTERS) {
-
include_once 'Zend/Service/Twitter/Exception.php';
-
throw new Zend_Service_Twitter_Exception( -
'Status must be no more than ' -
. self::STATUS_MAX_CHARACTERS
-
. ' characters in length' -
);
-
} elseif (0 == $len) {
-
include_once 'Zend/Service/Twitter/Exception.php';
-
throw new Zend_Service_Twitter_Exception( -
'Status must contain at least one character' -
);
-
} -
-
$data = array('status' => $status);
-
$remove = "";
-
if ($url && iconv_strlen($url, 'UTF-8') > 0) {
-
-
$photo = $this->get_web_page($url);
-
$photo = $photo['url'];
-
echo "The Photo Url is $photo";
-
-
$photo = file_get_contents($photo);
-
$t = explode(".", $url);
-
-
$ext = strtolower($t[(sizeof($t) - 1)]);
-
-
if($ext != "jpg" && $ext != "png" && $ext != "jpeg" && $ext != "gif")
-
$ext = "jpeg";
-
echo "What is the size of photo " . sizeof($photo) . "</br>";
-
echo "</br>";
-
$remove = "MettyaPhoto1.$ext";
-
$bytes = file_put_contents($remove, $photo);
-
echo "How many bytes were put $bytes </br>";
-
echo "Does the file exists " . file_exists($remove);
-
$mediaphoto = "MettyaPhoto";
-
$data['media[]'] = "@{$remove};type=image/$ext;filename={$mediaphoto}";
-
} -
$response = $this->_post($path, $data);
-
if(file_exists($remove))
-
unlink($remove);
-
return new Zend_Rest_Client_Result($response->getBody());
-
}

Replies
This is the error I receive from the xml object:
(())I'm not familiar enough with Zend to know whether it's handling other details for you -- do you know how it's doing OAuth in this context as well as how it is presenting the mixed content HTTP request? Are you able to use this same library to make basic requests and POST-based requests on api.twitter.com without the complication of file upload?
The same library definitely works making basic requests without a file upload:
This is the status update function
{'Status must be no more than ''Status must contain at least one character'}}}Im pretty sure the Oauth signature works without a problem because using the code for a simple status update works fine. And actually if I post a tweet that has the t.co url in it...and that same tweet has an image attached...using the statusUpdate function will actually post a tweet with that image.
The post function calls a prepare function that basically checks that the URI is set properly.
Then it calls a function called performPost that would use the
$datavariablethat was passed in the
$response = $this->_post($path, $data);This is the performPost function that uses the
$datavariable{}}So I updated the _performPost function with a safe_encode function that I borrowed from the TmhOauth library. https://github.com/themattharris/tmhOAuth/blob/master/tmhOAuth.php
The new _performPost function now looks like this:
{}}}}}But now Im getting this error saying that my tweet text is too long, even though I check the text length before I actually send the tweet. So strange things are happening
Does the file exists 1Does the file exists 1(())1I have another post showing how I updated the performPost function but it is awaiting moderation
I figured that the text of the tweet was getting extending past the length of 132 when I passed it through the safe_encode method and it found a url. So I put a url cleaner to remove all urls from the text, and now we're back to the same error of..."error creating status"
Does the file exists 1Does the file exists 1(())Are you actually uploading a photo when posting this link to an instagr.am photo? Keep in mind that when you use update_with_media you need to be sending an actual image -- in most cases you won't be including a URL within the post. In all cases, remember that the photo you're uploading with the method will be at least a 20 character URL within the tweet body as well, so your text needs to accomodate for that.
Yes. Uploading a photo for sure. So Im going to post the entire function, and the resulting
response. You will see that the text that is passed originally is 129 (too long) but after I pull out all urls...the text is 108. So I am getting an error creating status message.
{'Status must be no more than ''Status must contain at least one character'}}}The RESPONSE:
How long is the text of the tweet 108Does the file exists 1Post Parameters:([media[]] => @MettyaPhoto1.jpeg;type=image/jpeg;filename=MettyaPhoto1.jpeg)Zend_Rest_Client_Result Object([_sxml:protected] => SimpleXMLElement Object([request] => /1/statuses/update_with_media.xml[error] => Error creating status.)[_errstr:protected] =>)Specifically these are the post parameters...my previous post I just post was queued for moderation.
([media[]] => @MettyaPhoto1.jpeg;type=image/jpeg;filename=MettyaPhoto1.jpeg)These are the headers:
()These are the headers from a successful tweet
()To this tweet in particular https://twitter.com/starlineventure/status/228552284609576960
When I change the enctype to multipart I get a cannot authenticate with oauth?
()(())1Your OAuth library might not know how to handle multipart uploads correctly -- OAuth only signs the post parameters of a x-www-form-urlencoded request but once you do a multipart upload, you're no longer sending data of that type. Therefore, OAuth only need concern itself with the request at large and its oauth_* parameters, omitting all consideration of the POST body. It's not possible from this snippet to determine if that's the cause of your OAuth problem in this case though.