This is related to here, but, as I don't currently have the reputation to comment there, I decided to ask my somewhat different question here.
I am writing a plugin to connect to a RESTful API (using Basic Authentication). I have been following the examples on the aforementioned link (above); however, the dot notation in calling the plugin is puzzling to me.
(note: prior to installing this custom plugin (which successfully installed), I installed the Guzzle plugin.)
When I leave the code as it appeared in the example ("_entry.html") at the link ('craft.theater.get'), I receive the error message here:
Fatal error: Class 'Craft\TheaterVariable' not found in craft\app\variables\CraftVariable.php on line 55
Is there a way to use 'craft.theater.get' successfully in the index.html template? Does 'theater' refer to the entire theater plugin, here?
Additionally, for looping through the XML, is there something in Craft CMS similar to the PHP SimpleXML Parser for looping through the XML nodes?
Thanks.
Here is the code (thanks to the link mentioned before):
Index.html (located in craft/templates/theater/ ):
{#
# Theater index template
# -------------------
#
# This template is loaded whenever http://example.com/news is
# requested, because it is located at Theater/index.html in your
# craft/templates/ folder.
#
# See this page for more details on how Craft routes requests:
# http://craftcms.com/docs/routing
#}
{% extends "_layout" %}
{% set title = "Theater" %}
{% block content %}
<h1>Theater</h1>
{# begin - plugin test detection and confirmation #}
{% if craft.theater is defined %}
Never fear... theater is here.
{% else %}
I'm missing my theater.
{% endif %}
{% set plugin = craft.plugins.getPlugin('theater', false) %}
{{ plugin.isEnabled() ? 'plugin enabled' : 'plugin disabled'}}<br/>
{{ plugin.isInstalled() ? 'plugin installed' : 'plugin not installed'}}<br/>
{{ craft.theater is defined ? 'plugin true' : 'plugin false' }}
{# end - plugin test detection and confirmation #}
{# below - I commented out the version with 'craft.theater.get'... when I replaced it with 'craft.guzzle.get' it worked, but unsure why #}
{# set showtimes = craft.theater.get({ url: 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22nome%2C%20ak%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys' }) #}
{% set showtimes = craft.guzzle.get({ url: 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22nome%2C%20ak%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys' }) %}
{# below - commented out original for statement 'showtimes.data' ... removed '.data' and it works #}
{# for show in showtimes.data #}
{% for show in showtimes %}
<h1>{{ show.created }}</h1>
{% endfor %}
{% endblock %}
The code for the plugin file: TheaterPlugin.php (located in craft/plugins/theater/ ):
<?php
namespace Craft;
class TheaterPlugin extends BasePlugin
{
/**
* @return mixed
*/
public function init()
{
parent::init();
}
/**
* @return mixed
*/
public function getName()
{
return Craft::t('TheaterVariable');
}
/**
* @return mixed
*/
public function getDescription()
{
return Craft::t('TheaterVariable');
}
/**
* @return string
*/
public function getDocumentationUrl()
{
return '???';
}
/**
* @return string
*/
public function getReleaseFeedUrl()
{
return '???';
}
/**
* @return string
*/
public function getVersion()
{
return '1.0.0';
}
/**
* @return string
*/
public function getSchemaVersion()
{
return '1.0.0';
}
/**
* @return string
*/
public function getDeveloper()
{
return 'jw';
}
/**
* @return bool
*/
public function hasCpSection()
{
return false;
}
/**
*/
public function onBeforeInstall()
{
}
/**
*/
public function onAfterInstall()
{
}
/**
*/
public function onBeforeUninstall()
{
}
/**
*/
public function onAfterUninstall()
{
}
public function get($options){
$url = $options['url'];
$limit = array_key_exists('limit', $options) ? NumberHelper::makeNumeric($options['limit']) : null;
$offset = array_key_exists('offset', $options) ? NumberHelper::makeNumeric($options['offset']) : 0;
try {
//API Credentials
//$login = 'USERNAME';
//$password = 'PASSWORD';
$client = new \Guzzle\Http\Client();
$request = $client->get($url);
//cURL Options for Basic Authentication
$request->getCurlOptions()->set(CURLOPT_RETURNTRANSFER, true);
$request->getCurlOptions()->set(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$request->getCurlOptions()->set(CURLOPT_USERPWD, "$login:$password");
$response = $request->send();
if (!$response->isSuccessful()) {
return;
}
$items = $response->json();
$items = array_slice($items, $offset, $limit);
return $items;
} catch (Exception $e) {
return;
}
}
}