Posts tagged ‘PHP’

Blog now OpenID enabled

I’ve finally got around to allowing you to post a comment using an OpenID, by installing one of the WordPress OpenID plugins.

I had tried to do this over a year ago, but couldn’t quite get it working correctly. I had a number of problems before, notably that my hosting server only ran PHP4, whereas (at the time) most OpenID plugins seemed designed for PHP5, or a specific version of PHP4 that I wasn’t running. The other main factor was that most of the OpenID plugins at the time were very early (beta or earlier) versions, so may have been a little buggy still, whereas the plugin I installed today was very easy to get set up and running.

Anyone using a verified OpenID to post a comment on this site will find that their name and email is not required, plus your comments skip moderation.

Please let me know if you find any problems in using your OpenID on this site.

REST Service documentation with PHPDocumentor

I’ve just been hacking around with PHPDocumentor getting it to automatically create the documentation for the Cohere API and seems to be working well.

I have apilib.php which contains the PHP calls to the functions, then service.php is a wrapper around the apilib.php and provides the actual REST based services. Service.php is basically just a big switch statement, so, since PHPDocumentor needs the function declarations I can’t actually base the service documentation on service.php, so I’m using apilib.php as the basis for the service documentation.

I created my own PHPDocumentor template, which has been straightforward enough, though I did come across a couple of little issues – nothing too big and nothing that can’t be worked around .

These were the 2 little hacks I needed to put into my template:

  1. in apilib, I’ve named the functions using camel case, whereas the actual method calls in service.php need to be lower case – so I amended the stylesheet to use: {text-transform:lowercase;} where the function name was displayed
  2. I also needed to remove the dollar signs from the front of all the PHP parameters, so to do this I just used the Smarty templating command ‘replace’: {$functions[func].params[params].var|replace:’$’:”}

I still have some of the documentation to actually write into apilib.php – most notably example service calls for each method and double checking the return descriptions are consistent (and correct). All is working out well and you can see the generated docs on the Cohere site.

PHP arrays & recordsets to Javascript Objects (JSON)

Whilst writing the code for our MSG-Moodle integration, I needed to write a couple of helper functions to turn PHP arrays and ADOdb recordsets into JSON objects, and thought other might find them useful, so here they are:

This one takes an associative array of objects to turn into JSON object:

/**
* @param $objects object to turn into JSON
* @param $name overall name of the JSON object
* @param $callback name of the callback function
* @return string of the JSON object
*/
function phpToJSON($objects,$name,$callback=''){
$str = '';
if ($callback != ''){
$str .= $callback.'(';
}
if ($objects){
$str .= '{"'.$name.'":[';
$okeys = array_keys($objects);
for ($i=0; $i< sizeof($okeys); $i++){
$myobj = $objects[$okeys[$i]];
$attr = get_object_vars($myobj);
$str .= '{';
$keys = array_keys($attr);
for($j=0;$j< sizeof($keys); $j++){
$str .= '"'. $keys[$j] .'":"'. $attr[$keys[$j]] .'"';
if ($j != (sizeof($keys)-1)){
$str .= ',';
}
}
$str .= '}';
if ($i != (sizeof($objects)-1)){
$str .= ',';
}
}
$str .= ']}';
}
if ($callback != ''){
$str .= ');';
}
return $str;
}

and this one takes an ADOdb recordset and turns it into JSON object:

/**
* @param $objects object to turn into JSON
* @param $name overall name of the JSON object
* @param $callback name of the callback function
* @return string of the JSON object
*/
function phpRSToJSON($rs,$name,$callback=''){
$str = '';
if ($callback != ''){
$str .= $callback.'(';
}
if ($rs){

$str .= '{"'.$name.'":[';

$i=0;
while(!$rs->EOF) {
$i++;
$str .= '{';
$keys = array_keys($rs->fields);
for($j=0;$jfields[$keys[$j]] .'"';
if ($j != (sizeof($keys)-1)){
$str .= ',';
}
}
$str .= '}';
if ($i != $rs->RecordCount()){
$str .= ',';
}
$rs->MoveNext();
}

$str .= ']}';
}
if ($callback != ''){
$str .= ');';
}
return $str;
}

Hope you find these useful, and if you spot anything wrong (or that could be done better!) – please feel free to let me know 🙂

Curious handling of timeout by fsockopen??

We’ve been finding that sometimes my code using fsockopen doesn’t appear to be timing out in the given timeout period and I think I’ve tracked down the reason…

Essentially what’s been happening is that if the remote url is accessible (i.e. an fsockopen connection can be made to it), but the remote url takes a long time to respond, then the connection will just hang until a response is given – even if this exceeds the given timeout setting.

So if I have my timeout set to 10 seconds, but the remote URL takes 15 seconds to give a response, the script will NOT timeout after 10 seconds, instead it will wait until the remote URL responds. So my impression is that the timeout setting is only a connection timeout, not a general timeout – which was contrary to how I thought the timeout setting functioned. Thinking about if after this kinda makes sense, as the timeout is only on opening the connection, not on fopen – so it can’t know the remote url is taking longer than expected to respond.

As I can’t see a way to set a ‘real’ timeout on an fsock or fopen connection, I had a go using the cURL library. This appears to work in the way I expected – i.e. setting a timeout of 10 seconds would mean the script would stop after 10 seconds as the remote URL had not responded.

Thursday 10 August 06

I’ve just got my new laptop so am getting all the bits and bobs installed on here that I’ll need for the OCI work. I’m going through the process of installing Apache, MySQL, PHP etc (I’d prefer to do this myself than to use one of the all-in-one installers, just so I know how it all really works!). nearly there with it now, just a couple of things I noticed on various forums etc about getting PHP5 to work with Apache 2.2, a fair few forum postings mention downloading php5apache2_2.dll to use instead of php5apache2.dll. I found that I didn’t need to do this, and instead of adding a LoadModule command to the Apache httpd.conf file, I just added this at the end instead:

ScriptAlias /php/ “c:/PHP/”

AddType application/x-httpd-php .php .php5

Action application/x-httpd-php “/php/php-cgi.exe”

SetEnv PHPRC “C:/php”

I then also needed to update the directory security section in the httpd.conf file too, to allow Apache access to the PHP directory.

For info, this was the best tutorial I found for getting all this set up – although it’s for MySQL 4.1, it still holds for MySQL 5.

Thursday 20 July 06

Been looking further into creating a Moodle block for the MSG tool, and started to do a little bit of coding now. But soon came across the cross-domain problem when using XmlHttpRequest – in that you can’t make an AJAX request from one domain to another (well, not without the user lowering their browser security levels on their machine first – which wouldn’t be a good plan!)

So it looks like we’ve got 3 options: 1, install the software on the same server so the url is the same and everything will be happy, 2, make proxy http calls, or 3, use frames/iframes. Option 3 is pretty much ruled out straight away, and I;ve gone for testing out option 2 (as I’m not sure if we’ll be able to run the buddy space service on the same machine as Moodle).

So my first task for this was to figure out how to make http requests in PHP, for this I needed pear installed. I’d read that pear was included with PHP 5 but jsut needed activating, but I didn’t have go-pear.bat in my PHP installation. This was because I’d just downloaded the PHP 5 installer rather than the PHP 5 zip package. So I downloaded the zip package and unpacked over the top of my existing PHP 5 install (overwriting any files) and that appears to have worked fine, as I now have the go-pear.bat and necessary extensions – just need to actually install them now!

Thursday 6 April 06

Follow up discussion about the question of how to (or not to do!) XML parsing in different versions of PHP: http://moodle.org/mod/forum/discuss.php?d=43241

Wednesday 5 April 06

Getting on well with the development of the webservices for the Enterprise databases, and ought to have this about finished later this week. Then next week I can look at seeing how this maps across to the Enterprise SDK – I can use the same database and should to be able to reuse a lot of the code too (esp the parts that make calls to the database to retrieve data).

Another thing we’ve been looking at at is how we’ll do the XML parsing in Moodle using php. Most Moodle installations run on PHP 4 which doesn’t have built in XML functions, whereas PHP 5 does. There are libraries available for PHP 4 to do the XML parsing, but these (I think) would mean rewriting the code if we (and/or Moodle) moved to PHP 5. The alternative (so code didn’t need to be rewritten for different versions of PHP) would be to use regular expressions on the XML string – but that seems like a bit of a kludge to me!