Adding Update Profile Image functionality to Zend_Service_Twitter.

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Adding Update Profile Image functionality to Zend_Service_Twitter.

James Helly
Hi People,
Just been banging my head over getting the twitter service to update a profile image for a day! I've seen many many posts of people attempting this with other libraries with no joy.

I have come up with a basic solution that the community may want to improve apon :D

Seems like the Zend_Http_Client::request method could be allowed to handle files attached outside of the 'files' param for services that require file uploads to a non standard post variable.

Start Code
        Extend Twitter class to add methods account/update_profile_image
Author: James A Helly.

class  Application_Service_Twitter extends Zend_Service_Twitter {

     * Update Profile Image
     * @throws Zend_Http_Client_Exception if HTTP request fails or times out
     * @return Zend_Rest_Client_Result
    public function accountUpdateProfileImage($data) {
        //Get HTTP OAuth Client.
        $client = $this->_localHttpClient;
        //Set Path to Method.
        $path = '/1/account/update_profile_image.xml';
        $uri = '';
                //Manually Build the response body ala files, but its called image for this method.
        $body = "";
        $boundary = '---ZENDHTTPCLIENT-' . md5(microtime());
  $client->setHeaders(Zend_Http_Client::CONTENT_TYPE, Zend_Http_Client::ENC_FORMDATA . "; boundary={$boundary}");
        $fhead = array(Zend_Http_Client::CONTENT_TYPE => 'image/'.$data['type']);
        $body .= Zend_Http_Client::encodeFormData($boundary, 'image', $data['image'], 'profile.jpg', $fhead);
        $body .= "--{$boundary}--\r\n";
                //Go get the response.
                $response = $client->request(Zend_Http_Client::POST);
        return new Zend_Rest_Client_Result($response->getBody());


End Code

Let me know if you find it useful.