One approach to Avatar and Cover image juggling for AppDotNetPHP

Sunday, October 28, 2012 – 37 views

— by cdn

This isn't going to be pretty.

httpReq() isn't used since it gets in the way (hoist by own petard and all that ;))

With hints from http://curl.haxx.se/libcurl/php/examples/multipartpost.html

/** * MIME juggling for profile changes */ protected function updateUserImage($which = 'avatar', $avatar = null) {

$data = array($which=>"@$avatar"); // path reference only; no base64_encode() required

$req = $this->baseUrl.'users/me/' . $which; $ch = curlinit($req); $headers = array(); curlsetopt($ch, CURLOPTPOST, true); curlsetopt($ch, CURLOPTPOSTFIELDS, $data); $headers[] = "Content-Type: multipart/form-data"; if ($this->accessToken) { $headers[] = 'Authorization: Bearer '.$this->accessToken; } curlsetopt($ch, CURLOPTHTTPHEADER, $headers); curlsetopt($ch, CURLOPTRETURNTRANSFER, true); curlsetopt($ch, CURLINFOHEADEROUT, true); curlsetopt($ch, CURLOPTHEADER, true); if ($this->sslCA) { curlsetopt($ch, CURLOPTCAINFO, $this->sslCA); } $this->lastresponse = curlexec($ch); $this->lastrequest = curlgetinfo($ch,CURLINFOHEADEROUT); curlclose($ch); if ($this->lastrequest===false) { if (!curlgetinfo($ch,CURLINFOSSLVERIFYRESULT)) { throw new AppDotNetException('SSL verification failed, connection terminated.'); } } $response = $this->parseHeaders($this->lastresponse); $response = jsondecode($response,true); if (isset($response['error'])) { if (is_array($response['error'])) { throw new AppDotNetException($response['error']['message'], $response['error']['code']); } else { throw new AppDotNetException($response['error']); } } else { return $response; } }

Accompanied by a pair of specific functions:

    public function updateUserAvatar($avatar = null) {
            if($avatar != null)
                    return $this->updateUserImage('avatar', $avatar);
    }

    public function updateUserCover($cover = null) {
            if($cover != null)
                    return $this->updateUserImage('cover', $cover);
    }

Thank you for reading. (Assuming you even bothered)


1 Replies – 0 Reposts – 0 Stars


Discussion

Link to Conversation on ADN