Including PHP classes does not work.
This has me in an absolute furor - I know I've done one teeny tiny thing wrong, OR, I'm just completely missing how to do this altogether.
I'm trying to use the class iCalcreator from kigkonsult. If I include the class directly (a super huge problem because it's over 7000 lines) in a
<script type="text/php">...class code here...</script>
statement, the app loads the classes and makes them available…inside the same script tag (meaning I can't hide it at the bottom of the page or whatnot - meaning I have to scroll through 7000 lines of code when I accidentally hit the "Home" button). If, however, I attempt to include the script via an
include("iCalcreator.class.php");
statement in a script tag, the app debug tells me it can't find the file. I've tried several variations:
include("app://<appid>/iCalcreator.class.php");
include("./iCalcreator.class.php");
include("a.php"); //renamed, just to exclude possibility of typos
include("app://<appid>/a.php");
include "iCalcreator.class.php"; //which debug tells me it can't "include(1)"
I've checked permissions on the entire folder hierarchy, I've re-encoded the entire project via iconv and nada. The only feedback I get from the built-in console in developer is when I try
<script type="text/php" src="iCalcreator.class.php"></script>
the system tells me it's preprocessing the PHP, but still I end up with the error
Fatal error: Class '__kroll__namespace__1/vcalendar' not found in
- (the slash is actually the other way, but the forum is eating it…sorry I'm not helping seo!) every time….no, I don't get a reference to filename or line number…that's all the more I get. Sigh.
I am absolutely willing to admit I'm overlooking something, so I'm putting out a plea for better eyes than mine: Can someone puhlease tell me where I'm goofing up? Here's my current code that is failing so miserably:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/php" src="iCalcreator.class.php"></script>
<script type="text/php">
function getContents($file){
Titanium.API.debug("Got to PHP");
return file_get_contents($file);
}
function parseIcal($url){
if(!class_exists('vcalendar')){
include("iCalcreator.class.php");
}
$v = new vcalendar(); // create a new calendar instance
$v->setConfig( 'unique_id', 'kiveo.net' ); // set Your unique id, required if any component property UID is missing
$v->setProperty( 'method', 'PUBLISH' ); // required of some calendar software
/* start parse of remote file */
$v->setConfig( 'url', $url ); // iCalcreator also support remote files
$v->parse();
$v->sort(); // ensure start date order
$text = '';
foreach($v->components as $component=>$info){
if((int)$info->dtstart['value']['year']==2010){
$text .= $info->summary['value']."<br />";
}
if((int)$info->dtend['value']['year']>2009){
$text .= "nfound a winner!"."<br />";
}
if(count($info->rrule)){
$text .= "n found a repeater!"."<br />";
}
$text .= "-----------------------break-------------------------n"."<br />";
}
return $text;
}
</script>
<script type="text/javascript">
$(document).ready(function() {
var url;
$("#icalurl_submit").click(function(){
Titanium.API.debug("Getting Val");
url = $("#icalurl_text").val();
Titanium.API.debug("Got val:"+url);
var ics = parseIcal(url);
Titanium.API.debug("Called PHP. Returned:"+ics);
$("#ical_text").html(ics);
});
});
</script>
</head>
<body>
<fieldset id="icalurl_info">
<input type="text" id="icalurl_text" name="icalurl_text" value="http://www.google.com/calendar/ical/thisweekintryon.com_ano6p38ofa78ekpk7964l9cgqc@group.calendar.google.com/public/basic.ics" />
<input type="submit" id="icalurl_submit" name="icalurl_submit" value="submit" />
</fieldset>
<div id="ical_text"></div>
</body>
</html>
(PS – sorry for the multiple code lines where I should have done them inline, but the forum (Kohana?) mis-translates angle brackets and removes other characters making it super hard to read)
Thx,
J
9 Answers
-
I'm not sure I know what you mean by "server PHP script" or "Titanium Javascript App"; as you can combine PHP and Javascript within the same Titanium application. The PHP script I listed (and gave a link to for reference) is nothing but a class framework for retrieving and parsing iCal files; there's nothing "server" specific about it. As for the type of Titanium app, I made a mistake in not clarifying that it is a Desktop app, not a mobile app. I'm trying to follow (syntactically, in any case) the examples posted in the documentation for PHP & Titanium Desktop.
My query - perhaps I should thank you for having me boil it down - pertains to not being able to include PHP scripts as the documentation states. Is this not working in 1.2.1? I've not been able to find anyone else reporting the same problems as I, so my assumption is I've not followed the examples properly or have missed a step - or perhaps my development environment (Mac OS X 10.6.3 -Snow Leopard running in 64bit mode - MacBook Pro) is missing something or possibly misconfigured. I've tried a reinstall of Titanium, but that did not change anything. Should I have certain permissions set? Is the key in the file encoding? I don't understand why this doesn't work (which is why I gave as detail rich an example as I did).
Thank-you.
-
This appears not to have been adequately answered here.
I've just included an external PHP class and got it working.
I was struggling with (I think) the same issues you were having then, like a light in the darkness - you know, the car that comes out of no where & pins you in its headlights just before hammering you - I remembered a little blurb (can't even remember where I read it now) about this.
Include an external file using <script type='text/php' src='extFile.php'></script>
The trick is that in extFile.php you can't have the opening & closing indicators (<? … ?>).
Just like when you include a JS script you don't have the opening & closing "<script>" tags.
I stripped out my <? ?> tags & the included PHP class works great.
-
Are you trying to use a server PHP script inside a Titanium Javascript App or am I confused?
-
Javascript, my friend, not PHP codes.
There is no PHP intepreter/parser/server or whatever you called it, to execute you php codes; unlike a Linux/Apache/MySQL/PHP (LAMP) server.
-
@Peter; Actually, PHP is built into the framework for desktop apps, so, yes, there is. I've been able to (as I made mention in the post) get the code to work under less than desirable circumstances. I would like to know how to get it to work as the documentation suggests it should.
-
Hi
Was the problem resolved? I am very interested to see if PHP code can be used in a Ti Mobile App!!
-
Using "Titanium.API.debug" within the PHP file is the first mistake that catches my eye. I don't believe you can use Titanium Javascript API methods in your PHP script, at least not that way.
The dots in Javascript objects/classes act as concatenation operators in PHP. PHP is most likely seeing that as three concatenated constants named Titanium, API, and debug. If those constants don't exist, it'll throw warnings, and when it realizes that it would be an invalid placement of the concatenated value, it's probably throwing a syntax error and exiting.
-
This indeed needs to be resolved. There are quite a few people down here who are wondering the exact same thing. It looks as if titanium on mac does not work properly.
If it means that we need to start developing on win, fine. But let us know whether there is an issue or not.
-
This works for me on a desktop application with JQuery, PHP and SQLite (SQLite3, not PDO which only works on Linux).
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script src="../jquery/jquery-1.5.2.js"></script> <title></title> <script type='text/php' src='./test1.php'></script> </head> <body> <script type="text/javascript"> alert(test(1,2)); </script> </body> </html>
CAUTION: Don't use the <?php ?> tag inside test1.php
function test($a,$b) { return $a+$b; }