2012年8月4日 星期六

來學yii framework(2)

上次到建好project了,咱們繼續吧,
打開project目錄底下protected/config,裡面有個main.php,
是關於這個project的一些基本設定,

來學yii framework(1)

平常在公司用的都是老闆架好的framework(PHP版的RoR,有夠威的),
但這玩意除了在公司之外就沒法用了,總不能偷出來吧?
又覺得乾乾的學PHP不如直接學一個framework,
就上網GOOGLE了一下,決定想要來玩玩看這個Yii,
不是這個Yii,

Yi名驚人

是這個Yii。


2012年6月28日 星期四

jQuery UI sortable

最近在用jQuery UI的sortable的時候碰到了個問題,
就是在拖動要排序的物件時整排元素都會被往下推,
查了一下好像純粹是個bug,但是好像也沒有修正的樣子?
可是我已經忘記在哪裡找到了 囧

解法:

jQuery('.xxxx').sortable({
    start : function(event, ui) {
          ui.placeholder.html(' ');
    }
});

不知道為什麼讓placeholder有個空白就沒事了,
至少問題解決了。

php & curl & session

最近在試著利用curl去取得某api回傳的結果,
過程是這樣的:
A網頁呼叫B網頁做登入,完成之後,
A網頁呼叫C網頁取得需要的資料,

但是取得C網頁資料的時候必須使用到B網頁登入後產生的SESSION的資料:

//中斷A網頁session寫入的動作
session_write_close();

//B與C網頁的url
$b_url = 'http://api.example.com/login';
$c_url = 'http://api.example.com/get_data';

//先登入B網頁
$b_ch = curl_init($b_url);
curl_setopt($b_ch, CURLOPT_POST, 1);
curl_setopt($b_ch, CURLOPT_RETURNTRASFER, 1);
curl_setopt($b_ch, CURLOPT_POSTFIELD, 'username=test&password=love5566');
curl_setopt($b_ch, CURLOPT_COOKIE, session_name().'='.session_id());
curl_setopt($b_ch, CURLOPT_TIMEOUT, 10);
curl_exec($b_ch);
curl_close();

//再去C網頁取得資料
$c_ch = curl_init($c_url);
curl_setopt($c_ch, CURLOPT_POST, 1);
curl_setopt($c_ch, CURLOPT_RETURNTRASFER, 1);
curl_setopt($c_ch, CURLOPT_COOKIE, session_name().'='.session_id());
curl_setopt($c_ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($c_ch);
curl_close();

$result便是C網頁回傳的網頁內容了,
關鍵在curl_setopt($c_ch, CURLOPT_COOKIE, session_name().'='.session_id()),
讓兩個curl在執行時用的都是同一個session_id的session,


中間這樣嘗試的時候發覺會一直卡住,查了之後才發現是被鎖住了,

參考網頁:http://www.smooka.com/blog/2009/07/24/maintaining-php-session-when-using-curl/

裡面說到:
What does session_write_close() do? It, ends the current session and store session data. Apparently, PHP does not like when multiple scripts play around with the session, so, it locks it. Putting session_write_close makes sure that your current session is stored so you can retrieve it and use it.


所以在前面加上了session_write_close()之後,問題就解決了,
不過對於一個API來說,在取得資料還必須丟session_id給他似乎哪裡怪怪的,
也許有安全上的疑慮?
我想應該弄個類似open id這種使用access token的驗證機制應該會像樣一點。