app.php
来自「Bug tracker, and reporter.」· PHP 代码 · 共 786 行 · 第 1/2 页
PHP
786 行
<?php/** * Zend Framework * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@zend.com so we can send you a copy immediately. * * @category Zend * @package Zend_Gdata * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License *//** * Zend_Gdata_Feed */require_once 'Zend/Gdata/Feed.php';/** * Zend_Gdata_Http_Client */require_once 'Zend/Http/Client.php';/** * Zend_Version */require_once 'Zend/Version.php';/** * Zend_Gdata_App_MediaSource */require_once 'Zend/Gdata/App/MediaSource.php';/** * Provides Atom Publishing Protocol (APP) functionality. This class and all * other components of Zend_Gdata_App are designed to work independently from * other Zend_Gdata components in order to interact with generic APP services. * * @category Zend * @package Zend_Gdata * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */class Zend_Gdata_App{ /** * Client object used to communicate * * @var Zend_Http_Client */ protected $_httpClient; /** * Client object used to communicate in static context * * @var Zend_Http_Client */ protected static $_staticHttpClient = null; /** * Override HTTP PUT and DELETE request methods? * * @var boolean */ protected static $_httpMethodOverride = false; /** * Default URI to which to POST. * * @var string */ protected $_defaultPostUri = null; /** * Packages to search for classes when using magic __call method, in order. * * @var array */ protected $_registeredPackages = array( 'Zend_Gdata_App_Extension', 'Zend_Gdata_App'); /** * Maximum number of redirects to follow during HTTP operations * * @var int */ protected static $_maxRedirects = 5; /** * Create Gdata object * * @param Zend_Http_Client $client * @param string $applicationId */ public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0') { $this->setHttpClient($client, $applicationId); } /** * Adds a Zend Framework package to the $_registeredPackages array. * This array is searched when using the magic __call method below * to instantiante new objects. * * @param string $name The name of the package (eg Zend_Gdata_App) * @return void */ public function registerPackage($name) { array_unshift($this->_registeredPackages, $name); } /** * Retreive feed object * * @param string $uri The uri from which to retrieve the feed * @param string $className The class which is used as the return type * @return Zend_Gdata_App_Feed */ public function getFeed($uri, $className='Zend_Gdata_App_Feed') { return $this->import($uri, $this->_httpClient, $className); } /** * Retreive entry object * * @param string $uri * @param string $className The class which is used as the return type * @return Zend_Gdata_App_Entry */ public function getEntry($uri, $className='Zend_Gdata_App_Entry') { return $this->import($uri, $this->_httpClient, $className); } /** * Get the Zend_Http_Client object used for communication * * @return Zend_Http_Client */ public function getHttpClient() { return $this->_httpClient; } /** * Set the Zend_Http_Client object used for communication * * @param Zend_Http_Client $client The client to use for communication * @throws Zend_Gdata_App_HttpException * @return Zend_Gdata_App Provides a fluent interface */ public function setHttpClient($client, $applicationId = 'MyCompany-MyApp-1.0') { if ($client === null) { $client = new Zend_Http_Client(); } if (!$client instanceof Zend_Http_Client) { require_once 'Zend/Gdata/App/HttpException.php'; throw new Zend_Gdata_App_HttpException('Argument is not an instance of Zend_Http_Client.'); } $useragent = $applicationId . ' Zend_Framework_Gdata/' . Zend_Version::VERSION; $client->setConfig(array( 'strictredirects' => true, 'useragent' => $useragent ) ); $this->_httpClient = $client; Zend_Gdata::setStaticHttpClient($client); return $this; } /** * Set the static HTTP client instance * * Sets the static HTTP client object to use for retrieving the feed. * * @param Zend_Http_Client $httpClient * @return void */ public static function setStaticHttpClient(Zend_Http_Client $httpClient) { self::$_staticHttpClient = $httpClient; } /** * Gets the HTTP client object. If none is set, a new Zend_Http_Client will be used. * * @return Zend_Http_Client */ public static function getStaticHttpClient() { if (!self::$_staticHttpClient instanceof Zend_Http_Client) { $client = new Zend_Http_Client(); $useragent = 'Zend_Framework_Gdata/' . Zend_Version::VERSION; $client->setConfig(array( 'strictredirects' => true, 'useragent' => $useragent ) ); self::$_staticHttpClient = $client; } return self::$_staticHttpClient; } /** * Toggle using POST instead of PUT and DELETE HTTP methods * * Some feed implementations do not accept PUT and DELETE HTTP * methods, or they can't be used because of proxies or other * measures. This allows turning on using POST where PUT and * DELETE would normally be used; in addition, an * X-Method-Override header will be sent with a value of PUT or * DELETE as appropriate. * * @param boolean $override Whether to override PUT and DELETE with POST. * @return void */ public static function setHttpMethodOverride($override = true) { self::$_httpMethodOverride = $override; } /** * Get the HTTP override state * * @return boolean */ public static function getHttpMethodOverride() { return self::$_httpMethodOverride; } /** * Set the maximum number of redirects to follow during HTTP operations * * @param int $maxRedirects Maximum number of redirects to follow * @return void */ public static function setMaxRedirects($maxRedirects) { self::$_maxRedirects = $maxRedirects; } /** * Get the maximum number of redirects to follow during HTTP operations * * @return int Maximum number of redirects to follow */ public static function getMaxRedirects() { return self::$_maxRedirects; } /** * Imports a feed located at $uri. * * @param string $uri * @param Zend_Http_Client $client The client used for communication * @param string $className The class which is used as the return type * @throws Zend_Gdata_App_Exception * @return Zend_Gdata_App_Feed */ public static function import($uri, $client = null, $className='Zend_Gdata_App_Feed') { $client->resetParameters(); $client->setHeaders('x-http-method-override', null); $client->setUri($uri); $client->setConfig(array('maxredirects' => self::getMaxRedirects())); $response = $client->request('GET'); if ($response->getStatus() !== 200) { require_once 'Zend/Gdata/App/HttpException.php'; $exception = new Zend_Gdata_App_HttpException('Expected response code 200, got ' . $response->getStatus()); $exception->setResponse($response); throw $exception; } $feedContent = $response->getBody(); $feed = self::importString($feedContent, $className); if ($client != null) { $feed->setHttpClient($client); } else { $feed->setHttpClient(self::getStaticHttpClient()); } return $feed; } /** * Imports a feed represented by $string. * * @param string $string * @param string $className The class which is used as the return type * @throws Zend_Gdata_App_Exception * @return Zend_Gdata_App_Feed */ public static function importString($string, $className='Zend_Gdata_App_Feed') { // Load the feed as an XML DOMDocument object @ini_set('track_errors', 1); $doc = new DOMDocument(); $success = @$doc->loadXML($string); @ini_restore('track_errors'); if (!$success) { require_once 'Zend/Gdata/App/Exception.php'; throw new Zend_Gdata_App_Exception("DOMDocument cannot parse XML: $php_errormsg"); } $feed = new $className($string); $feed->setHttpClient(self::getstaticHttpClient()); return $feed; } /** * Imports a feed from a file located at $filename. * * @param string $filename * @param string $className The class which is used as the return type * @param string $useIncludePath Whether the include_path should be searched * @throws Zend_Gdata_App_Exception * @return Zend_Gdata_Feed */ public static function importFile($filename, $className='Zend_Gdata_App_Feed', $useIncludePath = false) { @ini_set('track_errors', 1); $feed = @file_get_contents($filename, $useIncludePath); @ini_restore('track_errors'); if ($feed === false) { require_once 'Zend/Gdata/App/Exception.php'; throw new Zend_Gdata_App_Exception("File could not be loaded: $php_errormsg"); } return self::importString($feed, $className); } /** * GET a uri using client object * * @param string $uri * @throws Zend_Gdata_App_HttpException * @return Zend_Http_Response */ public function get($uri) { $client->setConfig(array('maxredirects' => self::getMaxRedirects())); $client->resetParameters(); $client->setHeaders('x-http-method-override', null); $client->setUri($uri); $response = $client->request('GET'); if ($response->getStatus() !== 200) { require_once 'Zend/Gdata/App/HttpException.php'; $exception = new Zend_Gdata_App_HttpException('Expected response code 200, got ' . $response->getStatus()); $exception->setResponse($response); throw $exception; } return $response; } /** * POST data with client object * * @param mixed $data The Zend_Gdata_App_Entry or XML to post * @param string $uri POST URI * @param array $headers Additional HTTP headers to insert. * @param string $contentType Content-type of the data * @param array $extraHaders Extra headers to add tot he request * @return Zend_Http_Response * @throws Zend_Gdata_App_Exception * @throws Zend_Gdata_App_HttpException * @throws Zend_Gdata_App_InvalidArgumentException */ public function post($data, $uri = null, $remainingRedirects = null, $contentType = null, $extraHeaders = null) { require_once 'Zend/Http/Client/Exception.php'; if ($remainingRedirects === null) { $remainingRedirects = self::getMaxRedirects(); } if ($extraHeaders === null) { $extraHeaders = array();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?