PHP – Facebook – 使用FQL 取得分享、點讚的次數



$fb_appid = "你的APPID";
$fb_secret    = "你的APP密碼";



$config['appId']    = $fb_appid;
$config['secret']    = $fb_secret;
$Facebook = new Facebook($config);






$fql = "SELECT     click_count, 
comment_count,
comments_fbid,
commentsbox_count,
like_count,
normalized_url,
share_count,
total_count, 
url 
FROM link_stat 
WHERE url = 'http://www.wondershow.tw/post_01.php?i=3240' "; //你要查詢的網址


$Fary['method']    = 'fql.query';
$Fary['query'] = $fql;
$LinkInfo = $Facebook->api($Fary);     

//print_r($LinkInfo); 
/*
 * 用Google Chrome 按 F12 查看原始碼,結果應該會是這樣:
    Array
    (
[0] => Array
(
[click_count] => 0
[comment_count] => 13
[comments_fbid] => 147508702070374
[commentsbox_count] => 0
[like_count] => 28
[normalized_url] => http://www.wondershow.tw/post_01.php?i=3240
[share_count] => 2
[total_count] => 43
[url] => http://www.wondershow.tw/post_01.php?i=3240
)
    )
*/


//由FQL的api取得應該會是陣列型態,並沒有經過JSON編碼,所以若我要取得欄位total_count的值:
echo $LinkInfo[0]['total_count']; //會得到 43

 
// 一般我們看到的分享數字或是點讚的數字,其實是total_count 喔!
//至於實際的點讚數是 like_count; 分享數是 share_count~ 可以自己比對看看

 


 

 

/*以下是關於FQL取得的欄位說明取自於官網 https://developers.facebook.com/docs/reference/fql/link_stat*/

Name 說明
click_count

number

The number of times Facebook users have clicked a link to the page from a share or like

comment_count

number

The number of comments users have made on the shared story

comments_fbid

number

The object_id associated with comments plugin comments for this URL. This can be used to query for comments using the comment FQL table

commentsbox_count

number

The number of comments from a comments plugin on this URL. This only includestop level comments, not replies

like_count

number

The number of times Facebook users have "Liked" the page, or liked any comments or re-shares of this page

normalized_url

string

The normalized URL for the page being shared

share_count

number

The number of times users have shared the page on Facebook

total_count

number

The total number of times the URL has been shared, liked, or commented on

url 

string

URL of the web page users can share

 

PHP – 指定日期相加

/*
 * 若我要計算2013-01-29加14天是幾月幾日
 * 配合兩次的strtotime(), 最後再用date(); 依照你要的需求輸出
*/

//指定要計算的日期時間
$set_time = "2013-01-29";

$T = strtotime($set_time); //將時間的字串形式轉成時間戳記
$T = strtotime("+14 day", $T); //指定要對這個時間的加減計算,第一個參數字串可參考官網
echo date("Y-m-d H:i:s", $T); //輸出成你要的格式

//寫成一排就長這樣: (漂亮但人類理解慢,不易維護)
echo date("Y-m-d H:i:s", strtotime("+14 day", strtotime($set_time)));

jQuery – ajaxfileupload 使用dataType 為json的問題

之前的這篇
 http://www.wretch.cc/blog/fdjkgh580580/11928850
 發現返回的json還是有許多問題存在,
不過今天我希望返回文字,而不是json與xml格式,那麼參數dataType該指定什麼呢?

 
 ajaxfileupload.js中看到約195行的這段:
————————————————————————————————

    uploadHttpData: function( r, type ) {
        var data = !type;
        data = type == "xml" || data ? r.responseXML : r.responseText;
        // If the type is "script", eval it in global context
        if ( type == "script" )
            jQuery.globalEval( data );
        // Get the JavaScript object, if JSON is used.
        if ( type == "json" ) {
            //eval( "data = " + data ); //這個應該是寫錯的….
//如果php格式輸出的json格式長這樣{"act" : "hellow"} 那這裡要使用單引號 "data = \' "+data+" \' "
//如果php格式輸出的json格式長這樣{'act' : 'hellow'} 那這裡要使用雙引號 "data = \" "+data+" \" "
//因為PHP的json_encode是{"act" : "hellow"}所以我下面使用單引號
eval("data = \' "+data+" \' ");

}
        // evaluate scripts within html
        if ( type == "html" )
            jQuery("<div>").html(data).evalScripts();


        return data;
————————————————————————————————

所以我們參數dataType只需要指定為 xml 、json、script、html、以及不輸入這五種以外,就可以返回一段html文字了~
等同於jQuery.post預設返回的方法~如以下所示:
 

$.ajaxFileUpload({
'url' :    "xxx.php",
secureuri :    false,
fileElementId    :    'uplphoto',
dataType : 'text', //非內鍵的參數
data :    {

},
success :     function (data, status) {
//這裡直接使用返回的data

console.log(data);

},
error :    function (data, status, e) {
alert("上傳失敗囉")
console.log(data);
}
})    

 

PHP – PHP有個超好用的寫法,為什麼一堆人要用模板


<?
 
function TEST($string){
    ?>
    <style>
div.aaa{ padding:5px; background:#333333; color:white;}
    </style>
   
    <div class="aaa"><?=$string?></div>
    <?
    }

TEST("哈囉,我是一段HTML+PHP+JAVASCRIPT+CSS");
/*
基本上PHP可在任何地方使用 『?> <?』 或 『<? ?>』
可以包任何的形式,執行上都沒問題,正如以上所寫。

 所以我不太明白為什麼一堆人會用模板(template)的寫法?
模板還要使用替換字串,又要把它包程.html檔,再用PHP逐一轉換字串,

除了大幅降低速度之外,開方速度又慢又不好改…..
像這樣,把需要用到的html包為一個function 或是 class,不是很方便嗎?
*/

?>

PHP – jQuery 使用ajaxFileUpload套件的錯誤

使用 AJAX 上傳套件(Jquery Ajax File Uploader Version 2.1) 時遇到錯誤,這邊有好心的網友提供了說明….
主要是jquery版本中被拿掉handleError()的關係,以及加解JSON格式時的錯誤問題。按照這裡修改就成功了!
如果不用JSON格式而改用xml格式,那就不會發生這些問題囉!

 
請大家先參考過官方的範例檔:ajaxfileupload.php
先簡單說明其中的主要程式碼架構….

    function ajaxFileUpload()
    {
$("#loading") 
.ajaxStart(function(){
$(this).show();//上傳時的動畫
})
.ajaxComplete(function(){
$(this).hide();//上傳完畢的動畫
});
//開始上傳的程式碼
$.ajaxFileUpload
(
{
url:'doajaxfileupload.php', //接收上傳的php程式碼
secureuri:false, //不太清楚這個,應該是加密的什麼東西吧
fileElementId:'fileToUpload', //你的的id
dataType: 'json', //格式可選用xml或json,基本上我習慣用json
data:{name:'logan', id:'id'}, //你要夾帶的參數
success: function (data, status) //當成功時呼叫的funciton
{
if(typeof(data.error) != 'undefined')
{
if(data.error != '')
{
alert(data.error);
}else
{
alert(data.msg);
}
}
},
error: function (data, status, e) //失敗時呼叫的funciton
{
alert(e);
}
}
)

return false; //記得要false避免表單送出

    }

以上是官方的js,把json改成xml沒問題,但若要用json請參考大陸網友的說明吧!

——————————————————————————————————————————————————-

 

<strong>在做ajaxFileUpload时,我也遇到这个问题,同时还有其它的问题,用了一下午的时间解决了:<br>问题1:如楼主所说,jQuery.handleError is not a function 原因是,经测试handlerError只在jquery-1.4.2之前的版本中存在,jquery-1.6 和1.7中都没有这个函数了,因此在1.4.2中将这个函数复制到了ajaxFileUpload.js中,问题解决<br>handleError: function( s, xhr, status, e ) 		{<br>// If a local callback was specified, fire it<br>		if ( s.error ) {<br>			s.error.call( s.context || s, xhr, status, e );<br>		}<br><br>		// Fire the global callback<br>		if ( s.global ) {<br>			(s.context ? jQuery(s.context) : jQuery.event).trigger( &quot;ajaxError&quot;, [xhr, s, e] );<br>		}<br>	},<br>问题2:一直得到error ,无法执行指定的success方法。通过追踪ajaxFileUpload的执行过程发现,在调用它自身的uploadHttpData函数时,当执行if(type==&quot;json&quot;) eval(&quot;data = &quot;+data);<br>会抛出异常,导致在处理异常的时候将status = &quot;error&quot; 因此一直执行error方法。<br>上网查询,得知eval函数是用来执行一段js代码,而并不是如我所想的反解json串<br>eval(&quot;data = &quot;+data);的意思是 将data 赋值给 data参数 ,但是当我返回给页面的是一个简单的字符串,比如&quot;OK&quot; ,时,这样写就抛出异常。最后改为 eval(&quot;data = &#92;&quot; &quot;+data+&quot; &#92;&quot; &quot;);即将返回的数据用双引号引起来当作字符串,然后赋给 data 。终于成功了。。。<br>贴出来,希望可以帮助到其他同样遇到这个问题的人。</strong>


from : http://zhidao.baidu.com/question/351817654.html

——————————————————————————————————————————————————-

後來我還遇到小問題….
就是callback的時候,php使用json_encode(),會發生錯誤,
所以請網友注意一下上面改寫的這段…..eval("data = \" "+data+" \" ");
 
//如果php格式輸出的json格式長這樣{"act" : "hellow"} 那這裡要使用單引號 "data = \' "+data+" \' "
//如果php格式輸出的json格式長這樣{'act' : 'hellow'} 那這裡要使用雙引號 "data = \" "+data+" \" "
//因為PHP的json_encode是{"act" : "hellow"}所以我使用單引號"data = \' "+data+" \' "