Merge pull request #3068 from kinglozzer/patch-1

DOCS: Minor fixes to RestfulService documentation
This commit is contained in:
Damian Mooyman 2014-04-29 09:04:29 +12:00
commit a46f5f133a

View File

@ -15,12 +15,12 @@ RestfulService (see [flickrservice](http://silverstripe.org/flickr-module/) and
### Creating a new RestfulObject ### Creating a new RestfulObject
:::php :::php
//example for using RestfulService to connect and retrive latest twitter status of an user. //example for using RestfulService to connect and retrive latest twitter status of an user.
$twitter = new RestfulService("http://twitter.com/statuses/user_timeline/user.xml", $cache_expiry ); $twitter = new RestfulService("http://twitter.com/statuses/user_timeline/user.xml", $cache_expiry );
$params = array('count' => 1); $params = array('count' => 1);
$twitter->setQueryString($params); $twitter->setQueryString($params);
$conn = $twitter->connect(); $conn = $twitter->request();
$msgs = $twitter->getValues($conn, "status"); $msgs = $twitter->getValues($conn, "status");
### Extending to a new class ### Extending to a new class
@ -45,12 +45,12 @@ RestfulService (see [flickrservice](http://silverstripe.org/flickr-module/) and
$service->httpHeader('Accept: application/xml'); $service->httpHeader('Accept: application/xml');
$service->httpHeader('Content-Type: application/xml'); $service->httpHeader('Content-Type: application/xml');
$peopleXML = $service->connect('/people'); $peopleXML = $service->request('/people');
$people = $service->getValues($peopleXML, 'user'); $people = $service->getValues($peopleXML, 'user');
// ... // ...
$taskXML = $service->connect('/tasks'); $taskXML = $service->request('/tasks');
$tasks = $service->getValues($taskXML, 'task'); $tasks = $service->getValues($taskXML, 'task');
@ -71,12 +71,12 @@ You can traverse throught document tree to get the values or attribute of a part
for example you can traverse for example you can traverse
:::xml :::xml
<entries> <entries>
<entry id='12'>Sally</entry> <entry id='12'>Sally</entry>
<entry id='15'>Ted</entry> <entry id='15'>Ted</entry>
<entry id='30'>Matt</entry> <entry id='30'>Matt</entry>
<entry id='22'>John</entry> <entry id='22'>John</entry>
<entries> </entries>
to extract the id attributes of the entries use: to extract the id attributes of the entries use:
@ -96,14 +96,14 @@ If you don't know the exact position of dom tree where the node will appear you
node.Recommended for retrieving values of namespaced nodes. node.Recommended for retrieving values of namespaced nodes.
:::xml :::xml
<media:guide> <media:guide>
<media:entry id="2030">video</media:entry> <media:entry id="2030">video</media:entry>
</media:guide> </media:guide>
to get the value of entry node with the namespace media, use: to get the value of entry node with the namespace media, use:
:::php :::php
$this->searchValue($response, "//media:guide/media:entry") $this->searchValue($response, "//media:guide/media:entry")
@ -116,25 +116,25 @@ If the web service returned an error (for example, API key not available or inad
could delgate the error handling to it's descendant class. To handle the errors define a function called errorCatch could delgate the error handling to it's descendant class. To handle the errors define a function called errorCatch
:::php :::php
// This will raise Youtube API specific error messages (if any). // This will raise Youtube API specific error messages (if any).
public function errorCatch($response){ public function errorCatch($response){
$err_msg = $response; $err_msg = $response;
if(strpos($err_msg, '<') === false) { if(strpos($err_msg, '<') === false) {
user_error("YouTube Service Error : $err_msg", E_USER_ERROR); user_error("YouTube Service Error : $err_msg", E_USER_ERROR);
} }
return $response; return $response;
} }
If you want to bypass error handling on your sub-classes you could define that in the constructor. If you want to bypass error handling on your sub-classes you could define that in the constructor.
:::php :::php
public function __construct($expiry=NULL){ public function __construct($expiry=NULL){
parent::__construct('http://www.flickr.com/services/rest/', $expiry); parent::__construct('http://www.flickr.com/services/rest/', $expiry);
$this->checkErrors = false; //Set checkErrors to false to bypass error checking $this->checkErrors = false; //Set checkErrors to false to bypass error checking
} }
## Other Uses ## Other Uses
@ -147,21 +147,21 @@ such as del.icio.us
Put something like this code in mysite/code/Page.php inside class Page_Controller Put something like this code in mysite/code/Page.php inside class Page_Controller
:::php :::php
// Accepts an RSS feed URL and outputs a list of links from it // Accepts an RSS feed URL and outputs a list of links from it
public function RestfulLinks($url){ public function RestfulLinks($url){
$service = new RestfulService($url); $service = new RestfulService($url);
$request = $service->request(); $request = $service->request();
$body = $request->getBody(); $body = $request->getBody();
$items = $service->getValues($body,"channel","item"); $items = $service->getValues($body,"channel","item");
$output = ''; $output = '';
foreach($items as $item) { foreach($items as $item) {
// Fix quote encoding // Fix quote encoding
$description = str_replace('&amp;quot;', '&quot;', $item->description); $description = str_replace('&amp;quot;', '&quot;', $item->description);
$output .= "<li><a href=\"{$item->link}\">{$item->title}</a><br />{$description}</li>"; $output .= "<li><a href=\"{$item->link}\">{$item->title}</a><br />{$description}</li>";
}
return $output;
} }
return $output;
}
Put something like this code in `themes/<your-theme>/templates/Layout/HomePage.ss`: Put something like this code in `themes/<your-theme>/templates/Layout/HomePage.ss`: