在joomla中制作网络服务的最佳方法是什么?

时间:2015-10-23 02:54:56

标签: php json web-services curl joomla

大家好,今天我有一个有趣的问题。

在joomla中制作网络服务的最佳方法是什么? 我正在尝试在joomla中创建一个Web服务,我遇到了以下问题:

在视图的控制器中: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);
    }
}
?>

问题是,如果我执行:curl http://wsn.jserver/index.php?option=com_services&format=json 要使用这项服务,请回复我

* 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

我该怎么做这项工作? 在joomla中制作网络服务的最佳方法是什么?

2 个答案:

答案 0 :(得分:0)

您检查了JoomlaTools Framework吗?从链接页面:

  

围绕HTTP协议设计。每个组件自动提供开箱即用的3级JSON REST API,无需额外编码。

答案 1 :(得分:0)

解决! 我发现了问题。

问题是因为joomla默认情况下会重定向以选择语言。 在我的情况下,我一直复制插件语言过滤器并验证不是选项=“com_services”

然后当我执行命令“curl -v http://wsn.jserver/index.php?option=com_webervices”时,响应是:

*   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"}

总之,问题是因为joomla要做重定向。要解决这个问题你可以破解插件languagefilter:plugins / system / languagefilter / languagefilter.php或者创建一个新的插件。

相关问题