SubClosure

{ Don't Repeat Yourself ! }

Browsing Posts in PHP

amfphp 1.9 annoyance

No comments

I love amfphp but here is one thing that has been annoying me for almost a year now since the release of amfphp 1.9 final.
Whenever I download amfphp for a new project and import it into PHP Eclipse I have to fix this little error first.

Obviously the current amfphp developers aren’t using eclipse.

Have you tried to create a paypal button and pass a return URL to paypal that includes some paramters and experienced that paypal will only return visitors to your main page, ignoring your GET parameters ?

something like:

<input type=”hidden” name=”return” value=”http://www.mypage.com/index.php?display=thankyou”>

will not work unless you also specify this parameter:

<input type=”hidden” name=”rm” value=”2″>

this will instruct paypal to post its own parameters as POST and keep your parameters as GET.

Or as explained in the Paypal documentation:

“a value of 2 means: the payer’s browser is redirected to the return URL by the POST method, and all transaction variables are also posted”

Hope this will help someone.

Here is an example  of how to access joomla session data from another php script.
This is particular useful for implementing an AMF gateway or other ajax functionality.

To access the Joomla! session variables from another script, the easiest way is to instantiate the joomla application in that script. Please note that this will only work if that script is not already using its own session.

Example Code:


define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define('JPATH_BASE', dirname(__FILE__) );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

$session =& JFactory::getSession();
$user = $session->get( 'user' );

In this example I’m assuming the script is located in the main joomla directory.
If the script sits in some subdirectory you’ll have to adjust the JPATH_BASE definition.

I have been trying to add new line breaks to text only emails with \n\n.
I discovered that it will only work when using double quotes (“) for the body string.
When using single quotes (‘)  \n\n will show in emails as such.
This is because PHP interprets single and double quotes in a different manner.

As stated in the PHP String Manual:

“Unlike the double-quoted  and heredoc syntaxes, variables and escape sequences for special characters will NOT be expanded when they occur in single quoted strings.”

I recently needed to replace all  Degree ( ° )  symbols with a dot ( . ) symbol in a String.

So I tried this: $result = str_replace("°" , "." , $mystr) ;

It did not work, because the String ($mystr) was encoded in UTF8, since it was read in from a file.
PHP could not properly compare the characters.

I fixed it by changing the default encoding of my string:
$mystr = utf8_encode  ( $mystr) ;

After that the str_replace function returned the expected result.

It seems to be a good idea to always declare the encoding when loading local or remote files in PHP.

In order to view and edit Mantis Tasks directly from Eclipse or Flex Builder
you need to install the Mylyn Plugin for eclipse first:

http://www.eclipse.org/mylyn/

Then install the Mylyn – Mantis Connector:

https://github.com/Mylyn-Mantis/mylyn-mantis

To add your mantis repository click on Window->Other Views in Eclipse.
Select the Mylyn Folder and add task list and task repositories.

Then go to the task repository view, right click and choose to add a new repository.

Now the trick is to enter the right URL to your Mantis service file.
Its normally found under http://yourdomain.com/mantis/api/soap/mantisconnect.php (since mantis 1.1.0)

If that’s done you can add a query by choosing a project and a filter (needs to be set up on the mantis web interface first) .

Now you can view and change your mantis tasks in the task view.

In order to read the direct output of a PHP script into a variable for later processing
it needs to be buffered.

a buffer can be started with:
ob_start();

then anything that would output data directly can be executed, for example:
include 'foo.php' ; ( which might contain something like: echo 'bar' ; )

then read the buffer into a variable:
$buffer= ob_get_contents();

and clean the buffer afterwards:
ob_end_clean();

That’s it. Now whatever foo.php was outputting is now stored in
$buffer for further processing.

especially useful when implementing templates with PHP.

With this quick line of code it’s possible to convert arrays into anonymous objects in PHP

$obj = (object) array('foo' => 'bar', 'property' => 'value');

This is actually quite useful when using Zend AMF to transfer objects from PHP to Flex or Flash,
without having to create classes for them.

Creating a new standard object in PHP 5 is simply achieved by using stdClass:

$obj = new stdClass;

another possible way:
$obj = (object) array()

yet another – slightly more complicated method to achieve the above:

class foo {}; //must be declared on top level
$object = new foo();

It’s a bit difficult to get this information from the PHP docs.

Get Adobe Flash player