顯示具有 php 標籤的文章。 顯示所有文章
顯示具有 php 標籤的文章。 顯示所有文章

2014年7月16日 星期三

Facebook PHP SDK 4.0 get long lived session/token for manage_pages

<?php

$appid = ''; // your AppID
$secret = ''; // your secret

// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication($appid ,$secret);
 
$session = new FacebookSession($_SESSION['demo_accessToken']);
$session_long_lived = $session->getLongLivedSession($appid, $secret);

$request = new FacebookRequest(
 $session_long_lived,
 'GET',
 '/me/accounts'
);
$response = $request->execute();
$graphArray = $response->getGraphObject()->asArray();

get it~ You can check it by Facebook debug tools

2014年7月9日 星期三

codeigniter 設定 Medoo database framework為library

Medoo為一個蠻好用的database framework 一直很想把它加進codeigniter使用 首先file medoo.php in application/libraries:
<?php
class medoo 
{
   protected $database_type = 'mysql';
   // For MySQL, MariaDB, MSSQL, Sybase, PostgreSQL, Oracle
   protected $server = 'localhost';
   ......略
}
之後就能夠直接取用此工具囉!~ ex:
<?php
Controller admin.php in application/controllers

class Admin extends CI_Controller
{
    function index()
    {
 $this->load->library('medoo','datebase_name'); //第二欄位可設params
 $data = $this->medoo->select('table_name', '*');
    }
}

2014年6月26日 星期四

php 將時間轉換成幾分鐘(秒鐘)前表示

unction time_tran($the_time){
   $now_time = date("Y-m-d H:i:s",time()+8*60*60);
   $now_time = strtotime($now_time);
   $show_time = strtotime($the_time);
   $dur = $now_time - $show_time;
   if($dur < 0){
    return $the_time;
   }else{
    if($dur < 60){
     return $dur.'秒前';
    }else{
     if($dur < 3600){
      return floor($dur/60).'分鐘前';
     }else{
      if($dur < 86400){
       return floor($dur/3600).'小時前';
      }else{
       if($dur < 259200){//3天内
        return floor($dur/86400).'天前';
       }else{
        return $the_time;
       }
      }
     }

2013年8月5日 星期一

php 將array弄亂(取代mysql order by rand()的簡易方法)

因為工作需要從肥大的文章資料表中找出資料並加以隨機排列
使用mysql 使用了以下的語法後
 "SELECT * FROM table ORDER BY rand() LIMIT 20"
 發現搜尋時間效益不彰,
因此將內容先行取出再隨機排列


<?php
//$article 為取得文章的array
//產生一組1~20的array
$numbers = range(1, 20);
//打亂此array
shuffle($numbers);
foreach ($numbers as $number) {
    echo $article[$number]['article_title'];
}

?> 

2013年5月4日 星期六

取得Facebook按讚數(以PHP為例)

我們經常在網站中增加社群元件,比如說Facebook Like Button
我們透過以下工具產生按讚代碼 https://developers.facebook.com/docs/reference/plugins/like/
但怎麼取得按讚的數量呢?其實相當簡單,我們丟給facebook api請他回答給我們 預設回傳格式為xml
<?php
  $source_url = "www.example.com/xxx.html"; //想知道按讚數的網址
  $url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=".urlencode($source_url );
  $get_xml = file_get_contents($url);
  $xml_result = simplexml_load_string($get_xml);
  $count = $xml_result->link_stat->total_count;
?> 
範例中我們取得的total_count為全部的按讚數(包含評論等等),請依實際需求使用。 
若是要改寫成json格式,也可以寫成這樣:
<?php
$fburl = 'www.example.com/xxx.html';
$json_string = file_get_contents('http://api.facebook.com/restserver.php?method=links.getStats&urls=' . urlencode($url));
//或是已知道該網址ids就可寫成 $json_string = file_get_contents('http://graph.facebook.com/?ids=' . $url);
$json = json_decode($json_string, true);
?> 
亦或是使用fql語法:
<?php
$fql  = "SELECT url, normalized_url, share_count, like_count, comment_count, ";
$fql .= "total_count, commentsbox_count, comments_fbid, click_count FROM ";
$fql .= "link_stat WHERE url = '".$url."'";
$apifql="https://api.facebook.com/method/fql.query?format=json&query=".urlencode($fql);
$fb_json=file_get_contents($apifql);
?> 

Jquery ajax 簡單範例(以PHP為例)

在實際開發網站時,ajax是經常被使用到的技巧,而jquery提供了一項簡單實現的方法,不囉嗦,直接看範例:

1.php的部分(desc.php)
我們預期它回覆一段文字

<?php
  echo "想要回覆的訊息!!!";
?> 
2.html及javascript的部分(ajax.html)
//引入jquery



這樣就行了嗎?是的,就是這麼簡單,僅僅幾行desc.php能夠收到ajax請求了 ,但我們如何檢驗呢?我們將程式碼改寫一下




假設情境:我們當然不可能沒事使用ajax,通常都會有所需求,因此要求參數要怎麼做呢? 這次我們加入參數,並將回傳內容顯示在某個div容器中


這樣就是最標準的GET傳遞了!有沒有覺得很熟悉? 而php的部分則改寫成這樣:
<?php
  if(isset($_GET['id'])){
    echo "我收到請求了";
  }
?> 
順利的話,你的網頁將成功透過Jquery ajax方法完成一次請求,並回傳訊息顯示於id名為div_name的div之中 若是多個參數你也可以這樣下:

更進階的話,可以使用jquery的.serialize()或是參數使用json格式 請自行參考其他高手的教學囉~小弟我獻醜了! 下一篇我將介紹其他的東東。