Using Pygments with PHP (Python in PHP) - php

Is it possible to use Python (specifically Pygments) with PHP? Currently, I have a phpBB forum that I'm developing for and JS Syntax Highlighters just haven't been working for me. There's already a GeSHI mod, but I want to develop something myself just for experience.
Also, would there be performance issues?

There is now a library for this at:
http://derek.simkowiak.net/pygments-for-php/

Pretty much the only way to perform that integration (with PHP as the dominant language) is to shell out. This means starting python manually every time you need it.
That can be a little slow if you need to do it a lot. You can mitigate this by creating the syntax hilite when posts are created or edited, not when viewing.

If you're interested in diving into Python, you could write an external script or server application to update new posts with syntax-highlighted code. If it were me, I'd retain the original code in one database column and place the syntax-highlighted version in another.
A simple script to update new posts in batches could run as a cron job at whatever interval you find ideal.
To support a near real-time scenario, you could write a server application that sits and waits to be notified of new posts one at a time. For example, upon processing a new post, the PHP application could send the highlighting application a message through an AMQP queue.

Related

Debug PHP Using WAMP and an IDE

I recently started web development. The course I took was to install WAMP and start developing right away. I used an atom text editor, this -combined with wamp- proved to be a very fast way to write client-side code(HTML, CSS, Javascript).
But when I started to write serverside PHP things got a little messy. I should probably explain my site's structure here.
I keep separate PHP, CSS, javascript files for every page on the client side, for the server side a have 2 different types of PHP files:
Files that only perform a specific operation on the database(For example returning "5 more answers"). These are always called by AJAX requests.
Files that load the page for the first time. These are only used when the user opens the page for the first time, they do necessary database queries and return the page. Later requests always go to the 1st type of PHP files.
Now regarding my problem. I debugged until now by printing variables to the screen with var_dump() or echoing. But this started to become too slow as the data I work with grew. I wonder if there is a way of debugging which will let me but a breakpoint in one of my PHP files. Then, when I open it on the browser, on the localhost I created using WAMP, will let me go through the PHP file step by step.
I have been dealing with this issue for 3 days, I tried to make it work with Eclipse IDE but couldn't find a way. Also, there seems to be no tutorials or Q&A on the internet regarding the issue.
Breakpoint debugging opens a whole new world, and is the natural step after var_dump() debugging. Not only does it speed up development, but it provides much more information about your code, as you can step through each line and see what values have been set at each step, and how they evolve as your program executes its code. This means you can track the entirety of the values at different stages with one run - imagine tracking all variables at each point using var_dump()!
Although choosing an IDE is a personal decision based on personal taste, i strongly recommend you try out PhpStorm. If you can get a student licence go for it.
PhpStorm has extensive documentation & tutorials on all features in the IDE, debugging is no exception:
https://www.jetbrains.com/help/phpstorm/configuring-xdebug.html
https://www.youtube.com/watch?v=GokeXqI93x8
I don't know of a specific solution to your issue. I'm not exactly sure what you're doing but as a quick tip, I find add the following snippet to the top of the file useful as it will highly error more easily rather than browser just say nope.
error_reporting(E_ALL);
ini_set('display_errors', 'On');
Hope this help you a bit.
I tried out what's recommended in comments and answers. I first tried Netbeans. To be fair it disappointed me. Download kept getting stuck at 100%, even for different versions. When I stopped downloading and went ahead to create a php project, there was missing parts I guess. I couldn't even manage to create a php project. But that might just be me not being able to do it.
Then I followed #leuquim's answer and #Alex Howansky's comment and downloaded PHPStorm. And I got it to work in no more than 20 minutes. I downloaded it with a student's licence. For people who want to use PHPStorm with WAMP here's a Youtube tutorial:
https://www.youtube.com/watch?v=CxX4vnZFbZU
One thing to note in the video is that, maker of the video chooses PHP Web Application in the Run Configurations. That has been changed to PHP Web Page.

Is there a utility in Symfony framework that will monitor for any changes in your source and automatically restart your server?

I'm new to Symfony php framework & I'm looking for utility like nodemon in Nodejs that automatically listens to changes done in codebase and updates/restart the server. Is there anyway to get this done ?
In php in general there is no such need: it is a script language meaning that every time script is requested it is interpreted from scratch (it's bit simplified truth). So every time you edit your php file it is "live" right away - no need of server restart (like in node.js, asp.net etc)

how to run a php file from another server?

i am trying to do a video converter that grabs some files from a html form and converts them.
i would like to do the conversion on another server, .11, since i don't want to overload the main server .10
i can set up a network folder between the two servers, /media, and have a convert.php on .11 that will run the ffmpeg command.
if i run that php file from .10 then will the video conversion process take resources from .11 or from .10 ? Seems to me that .10 will be affected even if the php file is on .11.
I could do a cron job, but i really don't want to.
For this project i am using zend framework
Any ideas how to solve this issue?
thanks
I would definitely recommend implementing a Queue for these kind of tasks. Your queue could be simply a MySQL database that maintains a list of outstanding tasks. The workers can check this database for any tasks to be run.
This will provide you with far more flexibility in terms of scaling up. Tomorrow, if you decide to add two more worker servers/systems, they will fit seamlessly into the queue-model.

Working PHP.exe program and developing a timer program?

Hey Friends
how can i develop an app which work in php.exe in my php folder and i need to know can i develop an app which will work continuously using this function and able to work it on my CPanel?
EDIT
what i mean is,a principle one of my friend said to me to develop an page which is having a timer in it,in every 5 min it will check in a rss page for looking is there any change or new item is added or not?if we develop some thing like this it is difficult for us to work the php program again and again right??so i need some thing like that, and i need help in that
If you'er trying to develop a gui app, you might want to have a look at winbinder or php-gtk.
Ig you're asking how to write a program....then the answer is a bit long to include here.
You can develop a PHP app that runs on the command line (launched by php.exe). This way, you will not have a restriction on the running time. But it will not be integrated in CPanel.
I have to admit I probably didnt understand the question completly, so my answer is probably a bit off ...
Edit:
After your update, it seems that what you need is a cron/scheduler job. The cron job will call your script at intervals you specify. Google: PHP cron jobs.
If you mean periodically by "work continuously" you can use cron jobs (or windows scheduler). If you mean really continuosly, you should build an app with service/daemon functionality. As friends here already pointed out, it's a bit lenghty to fit in this edit box.

How to isolate server disaster script in PHP?

Oh my goodness. I never thought that I will need to ask you this. But unfortunately yes, I need to!
I have a PHP written script of my own that uses ffmpeg-php. And ffmpeg-php is a bastard. For some input it works ok, but for some it crashes my whole PHP and server throws Internal Server Error 500. I've tried several times to update ffmpeg-php, ffmpeg itself and so on, but when for some input it works in version 0.5 in 0.6 it wont work. And what i need is be sure that rest of the script will be processed correctly. And now it does not, because when it comes to run toGDImage() on movie frame I have Internal Server Error 500 and no feedback why from any source.
So for peace of mind of my users I decided that I need to isolate this part of script that messes with ffmpeg-php. I need a way to assure that if something will go terribly wrong in this part, it rest will go on.
Try catch does not work because this is not a warning, nor a fatal error, it is a horrible server-disaster. So what are your suggestions?
I think about putting this script into another file called ffmpeg-php-process.php and call it via HTTP and read result, if it is Internal Server Error 500 - I will know that it was not ok.
Or are there any other, more neat ways to isolate disaster scripts in PHP?
Ps. Don't write that I need to diagnose or debug or find the source of the error. I'm not a damn beginner and I'm not a ffmpeg dev to mess in it's code, I need to make my users safe now, and it's everything that i care now.
If you're getting a 500 error, it's because an exception of some sort is being thrown at a level lower than that of PHP itself. Unless your code is spinning into some kind of infinite loop or hitting a recursion limit (and especially since it worked with version 0.5), there's a good chance that ffmpeg or ffmpeg-php is crashing and taking the instance of PHP that launched it down with it.
Frankly, there's nothing you can do from PHP.
Your best bet would be, since you've already got access to the server, to write the script in question using a language like Python. There's a ton of ffmpeg python plugins, so you shouldn't have a difficult time setting that up at all. Call your Python script from PHP and pull in the output from a file. What this will do is isolate PHP from your script failing. It'll also get you away from ffmpeg-php (which, at least to me, seems like an unholy combination).
If you're dead-set on using PHP (which I don't recommend), you can launch another PHP script using php-cli from your outward-facing PHP script and do the work from there (as you would with Python). Again, I highly recommend that you avoid this.
Hope this helps!
You could spawn a new process containing your php-ffmpeg script. There are some functions to do that: proc_open() as instance.
The documentation has a not bad example about it:
http://php.net/proc_open
I have something similar going with a convoluted, large, bulky legacy php-email system I support. When it got apparent that the email system was becoming it's own beast, we split it off as its own virtual server entirely. There's no separation like PHYSICAL separation. And hey, virtual servers are cheap....
On the plus side, you can start, restart, and generally destroy the separate server with little affect on the rest of your code. It may also have improved backup implications (isolate media and logic) Since going this route, we've not ever taken the main application server down.
However, it does create a connection challenge as now rather than working local you're going to have your server talking to another separated by at the very least a bit of wire in the same cabinet (hopefully)

Categories