0
많은 코드를 재사용하지 않기 위해 Facebook PHP API의 도우미 클래스를 작성했습니다. 도우미가 작동하지만 유일한 문제는 매우 느린 것입니다 .. 그리고 나는 또한 이유를 알아 냈습니다! 클래스를 초기화 할 때 생성자는 두 번 호출됩니다! 내 코드와이 클래스를 사용하는 다른 요소는 한 번만 호출합니다 (클래스 자체 내부의 내용입니다). 문제가 무엇인지 파악하는 데 도움주세요. 감사!내 애플리케이션의 도우미 만들기
class FbHelper
{
private $_fb;
private $_user;
function __construct()
{
// Initalize Facebook API with keys
$this->_fb = new Facebook(array(
'appId' => 'xxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxx',
'cookie' => true,
));
// set the _user variable
//
$this->doLog("Called Constructor");
//
$this->_user = $this->UserSessionAuthorized();
return $this;
}
function doLog($text)
{
// open log file <----- THIS GETS CALLED TWICE EVERY TIME I INITIALIZE THE CLASS!!
$filename = "form_ipn.log";
$fh = fopen($filename, "a") or die("Could not open log file.");
fwrite($fh, date("d-m-Y, H:i")." - $text\n") or die("Could not write file!");
fclose($fh);
}
function getUser() { return $this->_user; }
function getLoginUrl() { return $this->_fb->getLoginUrl(); }
function getLogoutUrl() { return $this->_fb->getLogoutUrl(); }
function UserSessionAuthorized()
{
// Checks if user is authorized, if is sends back user object
$user = null;
$session = $this->_fb->getSession();
if (!$session) return false;
try {
$uid = $this->_fb->getUser();
$user = $this->_fb->api('/me');
if ($user) return $user;
else return false;
}
catch (FacebookApiException $e) { return false; }
}
private function _rebuildSelectedFriends($selected_friends)
{
// Creates a new array with less data, more useful and less malicious
$new = array();
foreach ($selected_friends as $friend)
{
$f = array('id' => $friend['id'], 'name' => $friend['name']);
$new[] = $f;
}
return $new;
}
function GetThreeRandomFriends()
{
$friends = $this->_fb->api('/me/friends');
$n = rand(1, count($friends['data']) - 3);
$selected_friends = array_slice($friends['data'], $n, 3);
return $this->_rebuildSelectedFriends($selected_friends);
}
function UserExists($user_id)
{
try { $this->_fb->api('/' . $user_id . '/'); return true; }
catch (Exception $e) { return false; }
}
}
예 .. 저도 그렇게 생각하고 있습니다. FbHelper를 호출하는 사람을 추적 할 수있는 방법이 있습니까 ?? 감사합니다 – PirosB3
당신은 이것을 위해 debug_print_backtrace()를 사용할 수 있습니다. – StasM
답변 해 주셔서 감사합니다. 문제가 .. Codeigniter를 사용하고 클래스를 두 번로드 한 것을 발견했습니다! 모든 도움에 감사드립니다! – PirosB3