php - 使用 rest api 获取 json 或 XML 格式的 joomla 文章

标签 php joomla joomla-extensions

我有一个移动应用程序(实际上是 Cordova HTML 移动应用程序)和一个客户 Web 应用程序 (php) 以及一个主要的基于 joomla 的网站。 这个 jooma 网站是我系统的基础,我想在移动应用程序(使用 JavaScript)和我的网络应用程序中显示它的博客文章。

我可以使用 RSS 提要来显示标题,但它是有限的(某种程度上)。我知道 php 和一些关于为 joomla 创建简单组件/模块的知识。知道我认为如何以正确的方式从 dB 获取文章吗?

博客文章是公开的,我需要知道我必须如何从数据库中获取文章,或者是否有任何扩展?

我还需要按点击率和发布日期来订购文章。

最佳答案

我为我自己的 joomla 网站创建了一种类似的脚本,我在其中获取 2 个日期范围之间的文章。

我的解决方法是在 joomla 之外的自定义 php 文件中初始化 joomla 框架,该文件位于 joomla 文件夹的根目录中。

我将我的文件命名为 articleApi.php 并保存在 joomla 的根目录下(假设您的 joomla 是 3.5.x 或更高版本)

这是我的脚本:

我使用纪元时间戳来获取 2 个日期之间的文章,url 应该是:http://YOUR_JOOMLA_SITE.COM/articleApi.php?starttime=1503260194&endtime=1503519394

<?php
define( '_JEXEC', 1 ); //This will define the _JEXEC constant that will allow you to access the rest of the Joomla framework
define('JPATH_BASE', realpath(dirname(__FILE__)));
require_once ( JPATH_BASE . '/includes/defines.php' );
require_once ( JPATH_BASE . '/includes/framework.php' );
require_once ( JPATH_BASE . '/libraries/joomla/factory.php' );
require_once ( JPATH_BASE . '/components/com_content/helpers/route.php');

// Instantiate the application.
$app = JFactory::getApplication('site');
// Initialise the application.
$app->initialise();
// Now you can use all classes of Joomla
$db = JFactory::getDBO();
$doc = JFactory::getDocument();
jimport( 'joomla.application.module.helper' );
jimport( 'joomla.application.component.model' );
jimport( 'joomla.application.router' );
JModelLegacy::addIncludePath(JPATH_SITE.'/components/com_content/models');
$tags = new JHelperTags;

function isTimestamp($timestamp) {
    if(ctype_digit($timestamp) && strtotime(date('Y-m-d H:i:s',$timestamp)) === (int)$timestamp) {
        return true;
    }else{
        return false;
    }
}

$jinput = JFactory::getApplication()->input;
$rawStartDate = $jinput->get('starttime', null, 'int');
$rawEndDate = $jinput->get('endtime', null, 'int');
$startDate = JFactory::getDate($rawStartDate);
$endDate = JFactory::getDate($rawEndDate);

$dateDiff = date_diff($startDate,$endDate);



if(!isTimestamp($rawStartDate)){
    $error = new stdClass();
    $error->status=406;
    $error->name='Start Date/time Range is incorrect.';
    header('content-type: application/json; charset=utf-8');
    header("access-control-allow-origin: *");
    header('Content-Type: application/json');
    header("HTTP/1.1 406");
    echo(json_encode($error));
    jexit();
}

if(!isTimestamp($rawEndDate)){
    $error = new stdClass();
    $error->status=406;
    $error->name='End Date/time Range is incorrect.';
    header('content-type: application/json; charset=utf-8');
    header("access-control-allow-origin: *");
    header('Content-Type: application/json');
    header("HTTP/1.1 406 Not Acceptable");
    echo(json_encode($error));
    jexit();
}

if($rawStartDate > $rawEndDate){
    $error = new stdClass();
    $error->status=406;
    $error->name='start Date/time is greater than end date/time.';
    header('content-type: application/json; charset=utf-8');
    header("access-control-allow-origin: *");
    header('Content-Type: application/json');
    header("HTTP/1.1 406 Not Acceptable");
    echo(json_encode($error));
    jexit();
}


if($dateDiff->m > 1){
    $error = new stdClass();
    $error->status=406;
    $error->name="Range shoudn't be more than one month";
    header('content-type: application/json; charset=utf-8');
    header("access-control-allow-origin: *");
    header('Content-Type: application/json');
    header("HTTP/1.1 406");
    echo(json_encode($error));
    jexit();
}

$articles = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
$i=0;
$output = [];
$ArticleFinal = array();
$appParams = JFactory::getApplication()->getParams();
$articles->setState('params', $appParams);
$articles->setState('filter.published', 1);
$articles->setState('filter.date_filtering','range');
$articles->setState('filter.start_date_range',$startDate);
$articles->setState('filter.end_date_range',$endDate);
$articles->setState('filter.ordering','a.created');
$items   = $articles->getItems();

foreach ($items as $key => $item)
{
    /*echo "<pre>";
    print_r($item);
    echo "</pre>";*/
    $tags->getItemTags('com_content.article', $item->id);
    $item->category_title = $item->category_title;
    $item->slug     = $item->id . ':' . $item->alias;
    $item->catslug  = $item->catid . ':' . $item->category_alias;
    $item->link     = JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->catid, $item->language));
    //jexit($item->link);
    $ArticleFinal[$i]["articleId"]                  = $item->id;
    $ArticleFinal[$i]["title"]                      = $item->title;
    $ArticleFinal[$i]["ArticleUrl"]                 = JURI::root() . JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->catid, $item->language));
    $ArticleFinal[$i]["text"]                       = $item->introtext . $item->fulltext;
    $ArticleFinal[$i]["categoryName"]               = $item->category_title;
    $ArticleFinal[$i]["categoryUrl"]                = JURI::root() . JRoute::_(ContentHelperRoute::getCategoryRoute($item->catid));
    $ArticleFinal[$i]["createdDate"]                = $item->created;
    $ArticleFinal[$i]["modifiedDate"]               = $item->modified;
    $ArticleFinal[$i]["createdBy"]                  = JFactory::getUser($item->created_by)->name;
    $ArticleFinal[$i]["createdByEmail"]             = JFactory::getUser($item->created_by)->email;
    $ArticleFinal[$i]["modifiedBy"]                 = JFactory::getUser($item->modified_by)->name;
    $ArticleFinal[$i]["modifiedByEmail"]            = JFactory::getUser($item->modified_by)->email;
    foreach($tags->itemTags as $keyTags => $valueTags){
        $ArticleFinal[$i]["tags"][$keyTags]["tag".$keyTags]         = $valueTags->title;
        $ArticleFinal[$i]["tags"][$keyTags]["tag".$keyTags."_url"]  = JURI::root() . 'tag/'.$valueTags->tag_id.'-'.$valueTags->alias;
    }

    $image = json_decode($item->images);
    if($image->image_intro){
        $ArticleFinal[$i]["storyImages"]["imageUrl"] = JURI::root() . $image->image_intro;
    }else{
        $ArticleFinal[$i]["storyImages"]["imageUrl"] = JURI::root() . $image->image_fulltext;
    }
    $i++;
}

$output[0]["articles"] = $ArticleFinal;
$output[0]["count"] = $i;

header('content-type: application/json; charset=utf-8');
header("access-control-allow-origin: *");
header('Content-Type: application/json');
echo(json_encode($output));
?>

我希望这对你有用,你甚至可以使用我的方法来获得你的定制解决方案。编码愉快!

关于php - 使用 rest api 获取 json 或 XML 格式的 joomla 文章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36663857/

相关文章:

joomla - 如何在一个包中安装组件和路由插件?

PhpStorm - 如果名称为 "myclass.php",则无法打开 PHP 文件

javascript - wp_enqueue_script - 渲染页面源代码中的文件路径丢失 "/"

php - 为什么 Mobiledetect.net 脚本无法检测到 iPhone?

git - 开发工作流程,Joomla,GIT

php - 如何获取组件参数?

javascript - 在数据库中存储帖子显示顺序

php - 带有 4 个参数的 Joomla 逻辑 PHP 查询

sql - Joomla 表单字段类型 sql ="....WHERE condition"?

javascript - Retina 显示屏上的 Safari 无法正确渲染谷歌地图