Hi guys today I have a interesting question.
What's the best way to make a webservices in joomla? I'm trying to make a web services in joomla and I have following problem:
in the controller of the view: components/com_webservice/view/view.json.php
<?php
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.view');
class WebServicesViewServices extends JViewLegacy {
private $data;
function __construct($config = array()) {
JLoader::import('models.services', JPATH_COMPONENT);
$model = new WebServicesModelServices();
if ($model->errors) {
echo json_encode($model->errors);
jexit();
}else{
$this->data = array('iphone' => '5s','iphone' => '6','iphone' => '6s','iphone' => '6s plus');
}
parent::__construct($config);
}
function display($tpl = null) {
echo json_encode($this->data);
}
}
?>
The problem is, if I execute: curl http://wsn.jserver/index.php?option=com_services&format=json to consume this services, this response me
* Connected to wsn.jserver (127.0.0.1) port 80 (#0)
> GET /index.php?option=com_jserver HTTP/1.1
> Host: wsn.jserver
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 303 See other
< Date: Fri, 23 Oct 2015 02:40:37 GMT
< Server: Apache/2.4.16 (Unix) PHP/5.5.30
< X-Powered-By: PHP/5.5.30
< Set-Cookie: 4dbb8abeb5e7919ee73c8545901d5f62=d6ksd6e93t99q7hsk8cf10hq35; path=/; HttpOnly
< Set-Cookie: e909c2d7067ea37437cf97fe11d91bd0=DO
< Location: http://wsn.jserver/index.php?lang=es
< Content-Length: 0
< Content-Type: text/html; charset=utf-8
<
* Connection #0 to host wsn.jserver left intact
How can I do that this work? what is the best way to make webservices in joomla?
Have you checked the JoomlaTools Framework? From the linked page:
Designed around the HTTP protocol. Each component automatically provides a level 3 JSON REST API out of the box, no extra coding required.
Solved! I've found the problem.
The problem is because joomla to do a redirect to select language by default. In my case, I've been duplicate the plugin languagefilter and validated that no be the option="com_services"
then when I execute the command "curl -v http://wsn.jserver/index.php?option=com_webervices" the response is:
* Trying 127.0.0.1...
* Connected to wsn.pawad (127.0.0.1) port 80 (#0)
> GET /index.php?option=com_pawaservices HTTP/1.1
> Host: wsn.jserver
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Fri, 23 Oct 2015 18:48:09 GMT
< Server: Apache/2.4.16 (Unix) PHP/5.5.30
< X-Powered-By: PHP/5.5.30
< Set-Cookie: 4dbb8abeb5e7919ee73c8545901d5f62=6a8m8cdte288k3jp2kvefmfe07; path=/; HttpOnly
< Content-Length: 16
< Content-Type: text/html
<
* Connection #0 to host wsn.pawad left intact
{"iphone":"5s", "iphone":"6", "iphone":"6s", "iphone":"6s plus"}
In conclusion, the problem is because joomla to do a redirect. to solved this problem you can hack the plugin languagefilter: plugins/system/languagefilter/languagefilter.php or create a new plugin.