Last active
January 6, 2016 18:00
-
-
Save purefan/6318167 to your computer and use it in GitHub Desktop.
Basic library and sample html page showing how to create a basic app for PHPFox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FoxApp | |
{ | |
private $_sFoxUrl = null; | |
private $_sAppId = null; | |
private $_sToken = null; | |
public function __construct($sFoxUrl = null, $sAppId = null) | |
{ | |
if (!empty($sUrl)) | |
{ | |
$this->setUrl($sFoxUrl); | |
} | |
if (!empty($sAppId)) | |
{ | |
$this->setAppId($sAppId); | |
} | |
if (isset($_POST['token'])) | |
{ | |
$this->_sToken = $_POST['token']; | |
} | |
else if ($this->_sAppId != null && $this->_sFoxUrl != null) | |
{ | |
$this->getNewToken(); | |
} | |
} | |
/** Sets the url to the PHPFox url | |
*/ | |
public function setUrl($sUrl) | |
{ | |
if (function_exists('filter_var')) | |
{ | |
if (filter_var($sUrl, FILTER_VALIDATE_URL) !== false) | |
{ | |
$this->_sFoxUrl = $sUrl; | |
} | |
else | |
{ | |
throw new Exception('Invalid PHPFox url:' . $sUrl); | |
} | |
} | |
else | |
{ | |
$this->_sFoxUrl = $sUrl; | |
} | |
} | |
public function getUrl() | |
{ | |
return $this->_sFoxUrl; | |
} | |
public function setAppId($sAppId) | |
{ | |
$this->_sAppId = str_replace(array("'", '"', '/', '\\'),'', $sAppId); | |
} | |
public function getAppId() | |
{ | |
return $this->_sAppId; | |
} | |
/* | |
* Attempts to get a new token from the PHPFox site. | |
* Tokens are needed for every interaction between your app and the PHPFox site | |
*/ | |
public function getNewToken() | |
{ | |
if (!isset($_GET['key'])) | |
{ | |
throw new Exception('The PHPFox site did not send a key'); | |
} | |
if ($this->_sFoxUrl == null) | |
{ | |
throw new Exception('You must set the url to the PHPFox site before requesting a token'); | |
} | |
$sUrl = $this->_sFoxUrl . 'token.php?key=' . $_GET['key']; | |
$fCont = file_get_contents($sUrl); | |
$oToken = json_decode($fCont); | |
if (!isset($oToken->token)) | |
{ | |
throw new Exception('Not able to create a token.'); | |
} | |
else | |
{ | |
$this->_sToken = $oToken->token; | |
} | |
return $this->_sToken; | |
} | |
public function getToken() | |
{ | |
return $this->_sToken; | |
} | |
/* Handles an ajax request and talks to the PHPFox site*/ | |
public function doAjax() | |
{ | |
if (!isset($_POST['method'])) | |
{ | |
return false; | |
} | |
$oData = $this->doPost($_POST); | |
// $oData has a json object, you can check the output from the PHPFox site | |
// in your Firebug tab | |
print_r($oData); | |
} | |
private function doPost($aParams) | |
{ | |
$hCurl = curl_init(); | |
curl_setopt($hCurl, CURLOPT_URL, $this->_sFoxUrl . 'api.php'); | |
curl_setopt($hCurl, CURLOPT_HEADER, false); | |
curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($hCurl, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($hCurl, CURLOPT_POST, true); | |
curl_setopt($hCurl, CURLOPT_POSTFIELDS, $aParams); | |
$sData = curl_exec($hCurl); | |
curl_close($hCurl); | |
return json_decode(trim($sData)); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Enable error reporting to help us debug issues | |
error_reporting(E_ALL); | |
require_once('./app.class.php'); | |
$oApp = new FoxApp(); | |
$oApp->setUrl('url to the PHPFox site'); | |
$oApp->setAppId('your app id from when you created the App at the PHPFox site'); | |
$oApp->doAjax(); | |
?> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" | |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en"> | |
<head> | |
<title>Test Application</title> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> | |
<link rel="stylesheet" type="text/css" href="<?php echo $oApp->getUrl(); ?>static/style.php?app_id=<?php echo $oApp->getAppId(); ?>" /> | |
<script type="text/javascript"> | |
/*$(document).ready(function(){ | |
$('body').append('<iframe id="crossdomain_frame" src="<?php echo $oApp->getUrl(); ?>static/crossdomain.php?height=' + document.body.scrollHeight + '&nocache=' + Math.random() + '" height="0" width="0" frameborder="0"></iframe>'); | |
}); */ | |
function postStatus() | |
{ | |
var sUrl = 'http://' + window.location.host + '/' + window.location.pathname; | |
$.ajax(sUrl, | |
{ | |
data: | |
{ | |
token: '<?php echo $oApp->getNewToken(); ?>', | |
method: 'user.updateStatus', | |
user_status: $('#txtMind').val() | |
}, | |
error: function(jqXHR, txtStatus, errorThrown) | |
{ | |
alert(txtStatus + ':' + errorThrown); | |
}, | |
success: function(data, textStatus, jqHXR) | |
{ | |
$('#frmStatus').after('User status updated'); | |
}, | |
type: 'POST' | |
}); | |
} | |
</script> | |
</head> | |
<body> | |
<h1>Test Application - Uploading Photos</h1> | |
<div class="extra_info p_bottom_15"> | |
This sample app will post a status update to <?php echo $oApp->getUrl(); ?> | |
</div> | |
<form id="frmStatus"> | |
<div class="table"> | |
<div class="table_left"> | |
What's on your mind: | |
</div> | |
<div class="table_right"> | |
<input type="text" id="txtMind" /> | |
</div> | |
</div> | |
<div class="table_clear"> | |
<input type="button" value="Upload" class="button" onclick="postStatus();" /> | |
</div> | |
</form> | |
<!-- With this we tell PhpFox to adjust the dimensions of the iFrame so our app fits nicely --> | |
<script> | |
$('body').append('<iframe id="crossdomain_frame" src="<?php echo $oApp->getUrl(); ?>static/crossdomain.php?height=' + document.body.scrollHeight + '&nocache=' + Math.random() + '" height="0" width="0" frameborder="0"></iframe>'); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment