slideshare.php

来自「Bug tracker, and reporter.」· PHP 代码 · 共 611 行 · 第 1/2 页

PHP
611
字号
    /**     * Uploads the specified Slide show the the server     *     * @param Zend_Service_SlideShare_SlideShow $ss The slide show object representing the slide show to upload     * @param boolean $make_src_public Determines if the the slide show's source file is public or not upon upload     * @return Zend_Service_SlideShare_SlideShow The passed Slide show object, with the new assigned ID provided     */    public function uploadSlideShow(Zend_Service_SlideShare_SlideShow $ss, $make_src_public = true)    {        $timestamp = time();        $params = array('api_key' => $this->getApiKey(),                        'ts' => $timestamp,                        'hash' => sha1($this->getSharedSecret().$timestamp),                        'username' => $this->getUserName(),                        'password' => $this->getPassword(),                        'slideshow_title' => $ss->getTitle());        $description = $ss->getDescription();        $tags = $ss->getTags();        $filename = $ss->getFilename();        if(!file_exists($filename) || !is_readable($filename)) {            throw new Zend_Service_SlideShare_Exception("Specified Slideshow for upload not found or unreadable");        }        if(!empty($description)) {            $params['slideshow_description'] = $description;        } else {            $params['slideshow_description'] = "";        }        if(!empty($tags)) {            $tmp = array();            foreach($tags as $tag) {                $tmp[] = "\"$tag\"";            }            $params['slideshow_tags'] = implode(' ', $tmp);        } else {            $params['slideshow_tags'] = "";        }        $client = $this->getHttpClient();        $client->setUri(self::SERVICE_UPLOAD_URI);        $client->setParameterPost($params);        $client->setFileUpload($filename, "slideshow_srcfile");        try {            $response = $client->request('POST');        } catch(Zend_Http_Client_Exception $e) {            throw new Zend_Service_SlideShare_Exception("Service Request Failed: {$e->getMessage()}");        }        $sxe = simplexml_load_string($response->getBody());        if($sxe->getName() == "SlideShareServiceError") {            $message = (string)$sxe->Message[0];            list($code, $error_str) = explode(':', $message);            throw new Zend_Service_SlideShare_Exception(trim($error_str), $code);        }        if(!$sxe->getName() == "SlideShowUploaded") {            throw new Zend_Service_SlideShare_Exception("Unknown XML Respons Received");        }        $ss->setId((int)(string)$sxe->SlideShowID);        return $ss;    }    /**     * Retrieves a slide show's information based on slide show ID     *     * @param int $ss_id The slide show ID     * @return Zend_Service_SlideShare_SlideShow the Slideshow object     */    public function getSlideShow($ss_id)    {        $timestamp = time();        $params = array('api_key' => $this->getApiKey(),                        'ts' => $timestamp,                        'hash' => sha1($this->getSharedSecret().$timestamp),                        'slideshow_id' => $ss_id);        $cache = $this->getCacheObject();        $cache_key = md5("__zendslideshare_cache_$ss_id");        if(!$retval = $cache->load($cache_key)) {            $client = $this->getHttpClient();            $client->setUri(self::SERVICE_GET_SHOW_URI);            $client->setParameterPost($params);            try {                $response = $client->request('POST');            } catch(Zend_Http_Client_Exception $e) {                throw new Zend_Service_SlideShare_Exception("Service Request Failed: {$e->getMessage()}");            }            $sxe = simplexml_load_string($response->getBody());            if($sxe->getName() == "SlideShareServiceError") {                $message = (string)$sxe->Message[0];                list($code, $error_str) = explode(':', $message);                throw new Zend_Service_SlideShare_Exception(trim($error_str), $code);            }            if(!$sxe->getName() == 'Slideshows') {                throw new Zend_Service_SlideShare_Exception('Unknown XML Repsonse Received');            }            $retval = $this->_slideShowNodeToObject(clone $sxe->Slideshow[0]);            $cache->save($retval, $cache_key);        }        return $retval;    }    /**     * Retrieves an array of slide shows for a given username     *     * @param string $username The username to retrieve slide shows from     * @param int $offset The offset of the list to start retrieving from     * @param int $limit The maximum number of slide shows to retrieve     * @return array An array of Zend_Service_SlideShare_SlideShow objects     */    public function getSlideShowsByUsername($username, $offset = null, $limit = null)    {        return $this->_getSlideShowsByType('username_for', $username, $offset, $limit);    }    /**     * Retrieves an array of slide shows based on tag     *     * @param string $tag The tag to retrieve slide shows with     * @param int $offset The offset of the list to start retrieving from     * @param int $limit The maximum number of slide shows to retrieve     * @return array An array of Zend_Service_SlideShare_SlideShow objects     */    public function getSlideShowsByTag($tag, $offset = null, $limit = null)    {        if(is_array($tag)) {            $tmp = array();            foreach($tag as $t) {                $tmp[] = "\"$t\"";            }            $tag = implode(" ", $tmp);        }        return $this->_getSlideShowsByType('tag', $tag, $offset, $limit);    }    /**     * Retrieves an array of slide shows based on group name     *     * @param string $group The group name to retrieve slide shows for     * @param int $offset The offset of the list to start retrieving from     * @param int $limit The maximum number of slide shows to retrieve     * @return array An array of Zend_Service_SlideShare_SlideShow objects     */    public function getSlideShowsByGroup($group, $offset = null, $limit = null)    {        return $this->_getSlideShowsByType('group_name', $group, $offset, $limit);    }    /**     * Retrieves Zend_Service_SlideShare_SlideShow object arrays based on the type of     * list desired     *     * @param string $key The type of slide show object to retrieve     * @param string $value The specific search query for the slide show type to look up     * @param int $offset The offset of the list to start retrieving from     * @param int $limit The maximum number of slide shows to retrieve     * @return array An array of Zend_Service_SlideShare_SlideShow objects     */    protected function _getSlideShowsByType($key, $value, $offset = null, $limit = null)    {        $key = strtolower($key);        switch($key) {            case 'username_for':                $responseTag = 'User';                $queryUri = self::SERVICE_GET_SHOW_BY_USER_URI;                break;            case 'group_name':                $responseTag = 'Group';                $queryUri = self::SERVICE_GET_SHOW_BY_GROUP_URI;                break;            case 'tag':                $responseTag = 'Tag';                $queryUri = self::SERVICE_GET_SHOW_BY_TAG_URI;                break;            default:                throw new Zend_Service_SlideShare_Exception("Invalid SlideShare Query");        }        $timestamp = time();        $params = array('api_key' => $this->getApiKey(),                        'ts' => $timestamp,                        'hash' => sha1($this->getSharedSecret().$timestamp),                        $key => $value);        if(!is_null($offset)) {            $params['offset'] = (int)$offset;        }        if(!is_null($limit)) {            $params['limit'] = (int)$limit;        }        $cache = $this->getCacheObject();        $cache_key = md5($key.$value.$offset.$limit);        if(!$retval = $cache->load($cache_key)) {            $client = $this->getHttpClient();            $client->setUri($queryUri);            $client->setParameterPost($params);            try {                $response = $client->request('POST');            } catch(Zend_Http_Client_Exception $e) {                throw new Zend_Service_SlideShare_Exception("Service Request Failed: {$e->getMessage()}");            }            $sxe = simplexml_load_string($response->getBody());            if($sxe->getName() == "SlideShareServiceError") {                $message = (string)$sxe->Message[0];                list($code, $error_str) = explode(':', $message);                throw new Zend_Service_SlideShare_Exception(trim($error_str), $code);            }            if(!$sxe->getName() == $responseTag) {                throw new Zend_Service_SlideShare_Exception('Unknown or Invalid XML Repsonse Received');            }            $retval = array();            foreach($sxe->children() as $node) {                if($node->getName() == 'Slideshow') {                    $retval[] = $this->_slideShowNodeToObject($node);                }            }            $cache->save($retval, $cache_key);        }        return $retval;    }    /**     * Converts a SimpleXMLElement object representing a response from the service     * into a Zend_Service_SlideShare_SlideShow object     *     * @param SimpleXMLElement $node The input XML from the slideshare.net service     * @return Zend_Service_SlideShare_SlideShow The resulting object     */    protected function _slideShowNodeToObject(SimpleXMLElement $node)    {        if($node->getName() == 'Slideshow') {            $ss = new Zend_Service_SlideShare_SlideShow();            $ss->setId((string)$node->ID);            $ss->setDescription((string)$node->Description);            $ss->setEmbedCode((string)$node->EmbedCode);            $ss->setNumViews((string)$node->Views);            $ss->setPermaLink((string)$node->Permalink);            $ss->setStatus((string)$node->Status);            $ss->setStatusDescription((string)$node->StatusDescription);            foreach(explode(",", (string)$node->Tags) as $tag) {                if(!in_array($tag, $ss->getTags())) {                    $ss->addTag($tag);                }            }            $ss->setThumbnailUrl((string)$node->Thumbnail);            $ss->setTitle((string)$node->Title);            $ss->setLocation((string)$node->Location);            $ss->setTranscript((string)$node->Transcript);            return $ss;        }        throw new Zend_Service_SlideShare_Exception("Was not provided the expected XML Node for processing");    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?