2016-04-16 7 views
2

私はモバイルアプリ(実際にはCordova HTMLモバイルアプリ)と1つの顧客Webアプリケーション(php)と1つのメインjoomlaベースのウェブサイトを持っています。 このjoomaサイトは私のシステムの基盤となっており、私はJavaScriptを使ってモバイルアプリとウェブアプリケーションでブログ記事を見せたいと思っています。残りのAPIを使用してjsonまたはXML形式でjoomlaの記事を取得

私はRSSフィードを使って見出しを表示できますが、それは限られています(いくつかの方法があります)。私はPHPとjoomlaのための単純なコンポーネント/モジュールを作成する方法について少し知っている。私は正しい方法でdBから記事を得ることができると思いますか?

ブログの投稿は公開されています。私はデータベースから記事を取得する必要がありますか、それとも拡張子がありますか?

また、ヒットと公開日によって商品を注文する必要があります。

答えて

0

私は2つの日付範囲の間で記事を取得する私自身のjoomlaサイト用に同様の種類のスクリプトを作成しました。

私の解決策は、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)); 
?> 

私はこれがあなたのために働くホープ、あなたもあなたのカスタマイズされたソリューションを得るために、私のアプローチを使用することができます。ハッピーコーディング!

関連する問題