php擷取json檔案資料並動態修改網站頭部檔案meta資訊 --基於CI架構

來源:互聯網
上載者:User

標籤:des   style   http   io   ar   sp   for   on   檔案   

話不多說了。直接開始吧  (如果有中文。請注意json只認utf-8編碼)

 

 

首先你需要有一個json檔案資料

{
        "index": {
                 "title": "indexmytitle",
                 "keywords": "中文",
                 "content": "中文",
                "description": "中文"
           },


       "goods": {
               "title": "goodsmytitle",
                "keywords": "goodskeywords",
                 "content": "goodsmycontent",
                "description": "goodsmydes"
           },

        "shop": {
               "title": "shopmytitle",
                "keywords": "shopkeywords",
               "content": "shopmycontent",
               "description": "shopmudes",
              "description1": "shopmudes"
       }


}

然後呢。你需要在CI裡邊建一個公用類,,appliction/librarys目錄  Json.php

<?php if (!defined(‘BASEPATH‘)) exit(‘No direct script access allowed‘);

class Json {

      public function some_function($seo_category){
              $path = ‘http://127.0.0.1/項目名/public/json.json‘; //檔案路徑
              $json_result= file_get_contents($path); //把json檔案讀入一個字串。
              $json_array= json_decode($json_result,true); // json_decode對 JSON 格式的字串進行編碼,轉換成數組形式
              return $json_array[$seo_category]; //根據key值判斷返回結果
}

}

?>

如果上邊的看不明白的話。那就看這個,二者等價,不過這個比較麻煩。如果修改的話還有改這個類庫

<?php if (!defined(‘BASEPATH‘)) exit(‘No direct script access allowed‘);

class Json {

         public function some_function($seo_category){

               $path = ‘http://127.0.0.1/ttzkq/public/json.json‘; //檔案路徑
               $json_result= file_get_contents($path); //把json檔案讀入一個字串。
              $json_array= json_decode($json_result,true); // json_decode對 JSON 格式的字串進行編碼,轉換成數組形式

           //二維數組轉一維數組
            foreach($json_array as $k => $v){
                           $$k = $v;
                }
              if($seo_category == ‘index‘){
               return $index;
             }elseif($seo_category == ‘goods‘){
                return $goods;
              }elseif($seo_category == ‘shop‘){
               return $shop;
             }

     }
}

?>

 

然後再看控制器裡邊,,

public function __construct(){

    $this->load->library(‘json‘); //載入json資料類庫

}

下邊方法裡邊調用,並傳值到html視圖裡邊

$top[‘json_result‘]= $this->json->some_function(‘index‘);

$this->load->view("templates/top",$top);

 

最後看視圖

<meta name=‘keywords‘ content="<?php echo $json_result[‘keywords‘];?>" >
<meta name=‘content‘ content="<?php echo $json_result[‘content‘];?>" >
<meta name=‘description‘ content="<?php echo $json_result[‘description‘];?>">

 

原創作品。。僅供學習之用,

 

 

php擷取json檔案資料並動態修改網站頭部檔案meta資訊 --基於CI架構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.