/** * Retrieve a new sfFeed implementation instance, populated from a web feed. * The class of the returned instance depends on the nature of the web feed. * This method uses the sfWebBrowser plugin. * * @param string A web feed URI * * @return sfFeed A sfFeed implementation instance */ public static function createFromWeb($uri, $options = array()) { if (isset($options['adapter'])) { $browser = new sfWebBrowser(array(), $options['adapter'], isset($options['adapter_options']) ? $options['adapter_options'] : array()); } else { $browser = new sfWebBrowser(); } $browser->setUserAgent(isset($options['userAgent']) ? $options['userAgent'] : 'sfFeedReader/0.9'); if ($browser->get($uri)->responseIsError()) { $error = 'The given URL (%s) returns an error (%s: %s)'; $error = sprintf($error, $uri, $browser->getResponseCode(), $browser->getResponseMessage()); throw new Exception($error); } $feedString = $browser->getResponseText(); return self::createFromXml($feedString, $uri); }
// build the xmlrpc client require_once $project_rootdir . '/plugins/sfWebRPCPlugin/lib/vendor/IXR_Library.inc.php'; $client = new IXR_Client($project_baseurl . "/sfWebRPCPluginDemo/RPC2"); // query remote neoip-casti to pull cast_mdata $succeed = $client->query('add', 3, 7); $t->is($client->query('add', 3, 7), true, 'call to sfWebRPCPluginDemo::add succeed'); // if it failed, notify the error //if(! $succeed ) throw new Exception("Error doing xmlrpc to casti_srv_uri due to ".$client->getErrorMessage()); $t->is($client->getResponse(), 10, 'add 3+7 is 10'); $t->is($client->query('fctnamej_which_doesnt_exist'), false, 'call to unexisting function fails as expected'); /************************************************************************/ /* Test handler JSON */ /************************************************************************/ $t->diag('sfWebRPCPlugin JSON'); $b->get($project_baseurl . "/sfWebRPCPluginDemo/JSON?method=add&arg0=3&arg1=2"); $t->is($b->getResponseCode(), 200, 'http code 200 on "add" JSON handler'); $t->is($b->getResponseHeader("Content-Type"), "application/json", "mimetype is application/json on json call"); $t->is($b->getResponseText(), 5, "response is 5 (as expected for add(3,2))"); $b->get($project_baseurl . "/sfWebRPCPluginDemo/JSON?method=add&arg0=3"); $t->isnt($b->getResponseCode(), 200, 'http code NOT 200 "add" when wrong number of parameters'); /************************************************************************/ /* Test handler JSONP */ /************************************************************************/ $t->diag('sfWebRPCPlugin JSONP'); $b->get($project_baseurl . "/sfWebRPCPluginDemo/JSON?callback=cbsample&method=add&arg0=3&arg1=2"); $t->is($b->getResponseCode(), 200, 'http code 200 on "add" JSON handler'); $t->like($b->getResponseHeader("Content-Type"), "/text\\/javascript/", "mimetype is text/javascript on JSONP call"); $t->is($b->getResponseText(), 'cbsample("5")', 'response is cbsample("5")'); /************************************************************************/ /* Test handler XDOMRPC */ /************************************************************************/
{ return $this->requestMethod; } } $t = new lime_test($nb_test_orig * count($adapter_list), new lime_output_color()); foreach ($adapter_list as $adapter) { $t->diag('Testing ' . $adapter); $t->diag(''); /******************/ /* Initialization */ /******************/ $t->diag('Initialization'); $b = new sfWebBrowser(array(), $adapter); $t->is($b->getUserAgent(), '', 'a new browser has an empty user agent'); $t->is($b->getResponseText(), '', 'a new browser has an empty response'); $t->is($b->getResponseCode(), '', 'a new browser has an empty response code'); $t->is($b->getResponseHeaders(), array(), 'a new browser has empty reponse headers'); /*******************/ /* Utility methods */ /*******************/ $t->diag('Utility methods'); $b = new sfWebBrowser(array(), $adapter); $t->is($b->setUserAgent('foo bar')->getUserAgent(), 'foo bar', 'setUserAgent() sets the user agent'); $t->is($b->setResponseText('foo bar')->getResponseText(), 'foo bar', 'setResponseText() extracts the response'); $t->is($b->setResponseCode('foo 123 bar')->getResponseCode(), '123', 'setResponseCode() extracts the three-digits code'); $t->is($b->setResponseCode('foo 12 bar')->getResponseCode(), '', 'setResponseCode() fails silently when response status is incorrect'); $t->is_deeply($b->setResponseHeaders(array('HTTP1.1 200 OK', 'foo: bar', 'bar: baz'))->getResponseHeaders(), array('Foo' => 'bar', 'Bar' => 'baz'), 'setResponseHeaders() extracts the headers array'); $t->is_deeply($b->setResponseHeaders(array('ETag: "535a8-9fb-44ff4a13"', 'WWW-Authenticate: Basic realm="Myself"'))->getResponseHeaders(), array('ETag' => '"535a8-9fb-44ff4a13"', 'WWW-Authenticate' => 'Basic realm="Myself"'), 'setResponseHeaders() extracts the headers array and accepts response headers with several uppercase characters'); $t->is_deeply($b->setResponseHeaders(array('HTTP1.1 200 OK', 'foo: bar', 'bar:baz', 'baz:bar'))->getResponseHeaders(), array('Foo' => 'bar'), 'setResponseHeaders() ignores malformed headers'); /**************/ /* Exceptions */
protected final function printBrowserResponse(sfWebBrowser $b, $url, $printText = false) { echo "****************************\n"; echo "COULDN'T GET REMOTE LOCATION: " . $url . "\n"; echo "Response code: " . $b->getResponseCode() . "\n"; echo "Response headers: "; print_r($b->getResponseHeaders()); if ($printText) { echo "Response text: \n"; echo $b->getResponseText(); } echo "****************************\n"; echo "\n"; }
/** * Loads an image from a file or URL and creates an internal thumbnail out of it * * @param string filename (with absolute path) of the image to load. If the filename is a http(s) URL, then an attempt to download the file will be made. * * @return boolean True if the image was properly loaded * @throws Exception If the image cannot be loaded, or if its mime type is not supported */ public function loadFile($image) { if (preg_match('/http(s)?:\\//i', $image)) { if (class_exists('sfWebBrowser')) { if (!is_null($this->tempFile)) { unlink($this->tempFile); } $this->tempFile = tempnam('/tmp', 'sfThumbnailPlugin'); $b = new sfWebBrowser(); try { $b->get($image); if ($b->getResponseCode() != 200) { throw new Exception(sprintf('%s returned error code %s', $image, $b->getResponseCode())); } file_put_contents($this->tempFile, $b->getResponseText()); if (!filesize($this->tempFile)) { throw new Exception('downloaded file is empty'); } else { $image = $this->tempFile; } } catch (Exception $e) { throw new Exception("Source image is a URL but it cannot be used because " . $e->getMessage()); } } else { throw new Exception("Source image is a URL but sfWebBrowserPlugin is not installed"); } } else { if (!is_readable($image)) { throw new Exception(sprintf('The file "%s" is not readable.', $image)); } } $this->adapter->loadFile($this, $image); }