예제 #1
0
 static function parseV3($str)
 {
     $str = preg_replace('/[\\n\\r]/', ' ', $str);
     $url = 'http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=' . urlencode($str);
     $c = new sfWebBrowser();
     try {
         if (!$c->get($url)->responseIsError()) {
             $c->setResponseText(iconv('ISO-8859-1', 'UTF-8', $c->getResponseText()));
             $json = $c->getResponseText();
             $result = json_decode($json, true);
         } else {
             return null;
         }
     } catch (Exception $e) {
         // Adapter error (eg. Host not found)
         throw $e;
     }
     if ($result['status'] == 'OK') {
         $address = self::parseGeoArray($result);
         return $address;
     } else {
         return false;
     }
 }
 /******************/
 /* 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 */
 /**************/
 $t->diag('Exceptions');
 $b = new sfWebBrowser(array(), $adapter);
 try {
     $b->get('htp://askeet');
     $t->fail('get() throws an exception when passed an uri which is neither http nor https');
 } catch (Exception $e) {
     $t->pass('get() throws an exception when passed an uri which is neither http nor https');