Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Developer
Posts: 87
Registered: ‎02-26-2010
My Device: Bold 9000
My Carrier: Rogers
Accepted Solution

Working with PHP in Widgets

Hi, I just have a few questions regarding PHP with Widgets:

 

1. I've read in a few places that you can use PHP in Widgets, but when I tried to include an index.php in my Widget, it notified me that it wasn't supported. So I am assuming that I have to have my PHP stored on some remote server, and link it it using an anchor for example. (Also white list my domain).

 

If this works, am I still able to access those Widget APIs? I have a Javascript file, with some Blackberry Menu API, and would like to know if these still work when they're on my remote server.

 

2. If I have a remote PHP file, and a remote JS file will not work, how would I go about in accessing a local copy of the JS file that I have compiled with the widget?

 

The reason I'm asking these questions is that I have a lot of dynamic pages that I would like to see on my Widget, but would also like to have the Widget API at hand as well for menus and the like.

 

Thanks!

Retired
Posts: 3,708
Registered: ‎10-16-2008
My Device: Z10
My Carrier: Rogers

Re: Working with PHP in Widgets

1. I've read in a few places that you can use PHP in Widgets, but when I tried to include an index.php in my Widget, it notified me that it wasn't supported. So I am assuming that I have to have my PHP stored on some remote server, and link it it using an anchor for example. (Also white list my domain).

Correct... you cannot add a PHP page directly into your widget archive.  You can either bring it into your widget by first white listing the URL in the config.xml <access> elements and then navigating to your page via an anchor tag or setting the location.href via JavaScript.

 

You can also call your PHP scripts via AJAX if you are using them to return pieces of HTML or data.

 

If this works, am I still able to access those Widget APIs? I have a Javascript file, with some Blackberry Menu API, and would like to know if these still work when they're on my remote server

Yes, you are still able to access your Widget APIs from your server side pages.  Each API you wish to access must have its feature id declared for the whitelisted domain.  For example, if your page is located at "http://www.foo.com" and you want to have access to the blackberry.ui.menu API... your config.xml <access> element will look like the following:

 

 

<access uri="http://www.foo.com" subdomains="true">
  <feature id="blackberry.ui.menu" version="1.0.0"/>
</access>

 

 

This will allow any page that is loaded from the www.foo.com domain to access the menu APIs from JavaScript.  This lets you place the functional pieces of your widget where they make sense for your application.

 

There is another config.xml setting that you will likely be interested in as well.  You can set your own custom header value for a BlackBerry Widget.  This way from your server side PHP code you can check to make sure that it is your actual widget hitting your PHP script.  The attribute is called "rim:header" and is located in the root <widget> element.  You can check out the Developers Guide document for more details if you are interested.

 

2. If I have a remote PHP file, and a remote JS file will not work, how would I go about in accessing a local copy of the JS file that I have compiled with the widget?

Number 1) will work, however you can also do what you are asking for in (2).  Your server side code PHP page, which you have white listed and displayed in the widget ,has access to your local resources of the widget.  This is quite handy for common resources that you don't want to draw across the network such as images, css and JavaScript.  

 

You can link to these resources by setting their path in your HTML to "local:///myimage.png".  The "local:///" is a local path that starts at the root of the widget archive.  For instance if you wanted to load an image from the local widget it would look something like the following:

 

 

<img src="local:///images/myimage.png" />

 

 

Hopefully that helps Smiley Happy

 

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter
Developer
Posts: 87
Registered: ‎02-26-2010
My Device: Bold 9000
My Carrier: Rogers

Re: Working with PHP in Widgets

Great that answered practically everything!

Thanks!

 

P.S. I thought that by linking a remote PHP file my skin CSS problem would be solved, but it still isn't sadly Smiley Sad

New Contributor
Posts: 4
Registered: ‎12-21-2010
My Device: Not Specified

Re: Working with PHP in Widgets

i developed a widget and it is working fine in blackberry 5.0 and 6.0, but not working in 4.6 and below please i want to know how to make this code work in this versions.

 This is my index.html

 

 

<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
function showCD()
{
 var queryString = window.top.location.search.substring(1);
 var str=queryString;
if (str=="")
  {
  document.getElementById("showdiv").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("showdiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","http://www.mydomain.com/textrequest.php?state="+str,true);
xmlhttp.send();
}
</script>
</head>
<body onLoad="showCD();">
<div id='showdiv' style='margin-top:-1px;'></div>
</body>
</html>

 

 

 

New Contributor
Posts: 4
Registered: ‎01-14-2011
My Device: all
My Carrier: all

Re: Working with PHP in Widgets

please help

i listed my url in confing.xml<access> and navigate to my page via anchor tag, but when my page have to be loaded i get message 'The return page had no content typ, and therefor can't be processed'

i look everywhere and i cant find solution?

Please help

thank you

Developer
Posts: 87
Registered: ‎02-26-2010
My Device: Bold 9000
My Carrier: Rogers

Re: Working with PHP in Widgets

@b-2-studio:

 

For the page you're trying to load, what extension is it? And what does it display?

 

For example, you might not be sending the Content-Type header when the headers are being sent.

 

New Contributor
Posts: 4
Registered: ‎01-14-2011
My Device: all
My Carrier: all

Re: Working with PHP in Widgets

Hi

im trying to load php extension, it's display information from mysql database

Thank you

Developer
Posts: 116
Registered: ‎03-08-2011
My Device: Not Specified

Re: Working with PHP in Widgets

[ Edited ]

Correct... you cannot add a PHP page directly into your widget archive.  You can either bring it into your widget by first white listing the URL in the config.xml <access> elements and then navigating to your page via an anchor tag or setting the location.href via JavaScript.

 

How do you add a php page to the archive?  What do you need to add to the config.xml file in order to make a php page work? I am trying to create a self contained app that mainly uses php and javascript and i'd like to save files to a local DB as well.

 

TY