/** * Save a new or updated Atom entry. * * Save is used to either create new entries or to save changes to * existing ones. If we have a link rel="edit", we are changing * an existing entry. In this case we re-serialize the entry and * PUT it to the edit URI, checking for a 200 OK result. * * For posting new entries, you must specify the $postUri * parameter to save() to tell the object where to post itself. * We use $postUri and POST the serialized entry there, checking * for a 201 Created response. If the insert is successful, we * then parse the response from the POST to get any values that * the server has generated: an id, an updated time, and its new * link rel="edit". * * @param string $postUri Location to POST for creating new entries. * @return void * @throws Zend_Feed_Exception */ public function save($postUri = null) { if ($this->id()) { // If id is set, look for link rel="edit" in the // entry object and PUT. $editUri = $this->link('edit'); if (!$editUri) { /** * @see Zend_Feed_Exception */ require_once 'Zend/Feed/Exception.php'; throw new Zend_Feed_Exception('Cannot edit entry; no link rel="edit" is present.'); } $client = Zend_Feed::getHttpClient(); $client->setUri($editUri); if (Zend_Feed::getHttpMethodOverride()) { $client->setHeaders(array('X-HTTP-Method-Override: PUT', 'Content-Type: ' . self::CONTENT_TYPE)); $client->setRawData($this->saveXML()); $response = $client->request('POST'); } else { $client->setHeaders('Content-Type', self::CONTENT_TYPE); $client->setRawData($this->saveXML()); $response = $client->request('PUT'); } if ($response->getStatus() !== 200) { /** * @see Zend_Feed_Exception */ require_once 'Zend/Feed/Exception.php'; throw new Zend_Feed_Exception('Expected response code 200, got ' . $response->getStatus()); } } else { if ($postUri === null) { /** * @see Zend_Feed_Exception */ require_once 'Zend/Feed/Exception.php'; throw new Zend_Feed_Exception('PostURI must be specified to save new entries.'); } $client = Zend_Feed::getHttpClient(); $client->setUri($postUri); $client->setHeaders('Content-Type', self::CONTENT_TYPE); $client->setRawData($this->saveXML()); $response = $client->request('POST'); if ($response->getStatus() !== 201) { /** * @see Zend_Feed_Exception */ require_once 'Zend/Feed/Exception.php'; throw new Zend_Feed_Exception('Expected response code 201, got ' . $response->getStatus()); } } // Update internal properties using $client->responseBody; @ini_set('track_errors', 1); $newEntry = new DOMDocument(); $status = @$newEntry->loadXML($response->getBody()); @ini_restore('track_errors'); if (!$status) { // prevent the class to generate an undefined variable notice (ZF-2590) if (!isset($php_errormsg)) { if (function_exists('xdebug_is_enabled')) { $php_errormsg = '(error message not available, when XDebug is running)'; } else { $php_errormsg = '(error message not available)'; } } /** * @see Zend_Feed_Exception */ require_once 'Zend/Feed/Exception.php'; throw new Zend_Feed_Exception('XML cannot be parsed: ' . $php_errormsg); } $newEntry = $newEntry->getElementsByTagName($this->_rootElement)->item(0); if (!$newEntry) { /** * @see Zend_Feed_Exception */ require_once 'Zend/Feed/Exception.php'; throw new Zend_Feed_Exception('No root <feed> element found in server response:' . "\n\n" . $client->responseBody); } if ($this->_element->parentNode) { $oldElement = $this->_element; $this->_element = $oldElement->ownerDocument->importNode($newEntry, true); $oldElement->parentNode->replaceChild($this->_element, $oldElement); } else { $this->_element = $newEntry; } }
/** * Save a new or updated Atom entry. * * Save is used to either create new entries or to save changes to * existing ones. If we have a link rel="edit", we are changing * an existing entry. In this case we re-serialize the entry and * PUT it to the edit URI, checking for a 200 OK result. * * For posting new entries, you must specify the $postUri * parameter to save() to tell the object where to post itself. * We use $postUri and POST the serialized entry there, checking * for a 201 Created response. If the insert is successful, we * then parse the response from the POST to get any values that * the server has generated: an id, an updated time, and its new * link rel="edit". * * @param string $postUri Location to POST for creating new entries. * @throws Zend_Feed_Exception * @return void */ public function save($postUri = null) { if ($this->id()) { // If id is set, look for link rel="edit" in the // entry object and PUT. $editUri = $this->link('edit'); if (!$editUri) { throw new Zend_Feed_Exception('Cannot edit entry; no link rel="edit" is present.'); } $client = Zend_Feed::getHttpClient(); $client->setUri($editUri); if (Zend_Feed::getHttpMethodOverride()) { $client->setHeaders(array('X-HTTP-Method-Override: PUT', 'Content-Type: application/atom+xml')); $client->setRawData($this->saveXML()); $response = $client->request('POST'); } else { $client->setHeaders('Content-Type', 'application/atom+xml'); $client->setRawData($this->saveXML()); $response = $client->request('PUT'); } if ($response->getStatus() !== 200) { throw new Zend_Feed_Exception('Expected response code 200, got ' . $response->getStatus()); } } else { if ($postUri === null) { throw new Zend_Feed_Exception('PostURI must be specified to save new entries.'); } $client = Zend_Feed::getHttpClient(); $client->setUri($postUri); $client->setRawData($this->saveXML()); $response = $client->request('POST'); if ($response->getStatus() !== 201) { throw new Zend_Feed_Exception('Expected response code 201, got ' . $response->getStatus()); } } // Update internal properties using $client->responseBody; @ini_set('track_errors', 1); $newEntry = @DOMDocument::loadXML($response->getBody()); @ini_restore('track_errors'); if (!$newEntry) { throw new Zend_Feed_Exception('XML cannot be parsed: ' . $php_errormsg); } $newEntry = $newEntry->getElementsByTagName($this->_rootElement)->item(0); if (!$newEntry) { throw new Zend_Feed_Exception('No root <feed> element found in server response:' . "\n\n" . $client->responseBody); } if ($this->_element->parentNode) { $oldElement = $this->_element; $this->_element = $oldElement->ownerDocument->importNode($newEntry, true); $oldElement->parentNode->replaceChild($this->_element, $oldElement); } else { $this->_element = $newEntry; } }