g-i-o- / pyphp

Php parser, compiler and interpreter for python
MIT License
10 stars 4 forks source link

Absolutely No Useful Documentation #1

Closed ghost closed 9 years ago

ghost commented 9 years ago

Hi. I would like to request that you add a small piece of documentation (or, if it exists, make it more obvious) explaining how to use this. Please.

Update: It's been fixed. Thank you so much!

ghost commented 9 years ago

If you're wondering why I need this, I'm making an HTTP server software and want to include a PHP parser so it'll be even remotely useful. I was hoping to develop a plugin based on this, but I need to know how to

  1. Run a PHP file (passing the source code as an argument)
  2. Get the HTML output of said PHP file
  3. Pass URL arguments into this PHP file.

Any help would be much appreciated, and I will provide attribution in the ACKNOWLEDGEMENTS.md file. And of course use proper licensing (though I may need to adjust the license I've licensed it under to fit)

g-i-o- commented 9 years ago

Thanks, I haven't been working on this code in a while, but I've just added a usage section to the readme (and moving it to the package root so it shows up in github).

Also, I fixed a few old bugs and added support for redirecting stdout for the php script, as it previously was hard coded to python's sys.stdout. to easily keep track of it I started versioning the code, currently it's at v0.2.3.

In any case, you would have to import the pyphp.executer module and call the execute_php function with the php code, and use StringIO to capture the output. To pass url arguments to the script you would have to pass a global $_GET variable which would be a PHPArray with whatever contents you want it to have.

This code:

import pyphp.executer
import pyphp.phparray
import cStringIO

phpcode="<html><body><?php var_dump($_GET);  ?></body></html>";

stdout=cStringIO.StringIO()

phpglobals={
    "$_GET" : pyphp.phparray.PHPArray(
        ('drink' , 'coffee'),
        ('hot', True)
   )
}

pyphp.executer.execute_php(phpcode, phpglobals, stdout=stdout)

print stdout.getvalue()

should output:

<html><body>array(2) {
  ['drink']=>
  string(6) 'coffee'
  ['hot']=>
  bool(true)
}
</body></html>

Hope this helps.

ghost commented 9 years ago

Thanks! You're really awesome. I doubt I could write a PHP... thing like you did. Finding this was a lifesaver for me. Now, off to fix my license such that it allows me to use this as a plugin! :P

This issue should probably be closed now :)

ghost commented 9 years ago

OK, with a modified version of the code, I'm getting an error:

import pyphp.executer
import pyphp.phparray
import cStringIO

def main(script, args={}):
    phpcode=script

    stdout=cStringIO.StringIO()

    a = [(x, args[x]) for x in args]
    print a
    phpglobals={
        "$_GET" : pyphp.phparray.PHPArray(*a)
    }

    pyphp.executer.execute_php(phpcode, phpglobals, stdout=stdout)

    return stdout.getvalue()

That's modified so it's a valid plugin for my server. When I give it a valid PHP hello world, though:

>>>main('<?php echo "Hello, World"; ?>')
[]

Traceback (most recent call last):
  File "<pyshell#0>", line 1
    main('<?php echo "Hello, World"; ?>')
  File "C:\Users\Nathan\Desktop\Programming\Python\Web\Servers\HTTP-WalServeUs\plugins\php.py", line 16, in main
    pyphp.executer.execute_php(phpcode, phpglobals, stdout=stdout)
  File "C:\Users\Nathan\Desktop\Programming\Python\Web\Servers\HTTP-WalServeUs\plugins\pyphp\executer.py", line 556, in execute_php
    phpcode = parser.parse_php(phpcode)
  File "C:\Users\Nathan\Desktop\Programming\Python\Web\Servers\HTTP-WalServeUs\plugins\pyphp\parser.py", line 420, in parse_php
    return P.parse()
  File "C:\Users\Nathan\Desktop\Programming\Python\Web\Servers\HTTP-WalServeUs\plugins\pyphp\parser.py", line 128, in parse
    self.read_outcode()
  File "C:\Users\Nathan\Desktop\Programming\Python\Web\Servers\HTTP-WalServeUs\plugins\pyphp\parser.py", line 402, in read_outcode
    if len(outcode_text) > 0:
TypeError: object of type 'NoneType' has no len()

Am I doing something blatantly wrong?

g-i-o- commented 9 years ago

Oh, this must be a bug, i'll check on the code and let you know.

ghost commented 9 years ago

@g-i-o- Thanks.

g-i-o- commented 9 years ago

I moved this bug to its own issue #3, so as to open/close it properly. In any case, it was a parser bug.

ghost commented 9 years ago

@g-i-o- Great.

ghost commented 9 years ago

I'll see if I can get it to work now

ghost commented 9 years ago

I'd say there's useful documentation now.