PHP: Run function in background (and track it?) - php

I have several parts to this problem. First, I've got a website and I'm trying to import data from another system, but the import could take several minutes. Meanwhile I'd like to let the user go on and continue working on the website, running the import in the background.
I'm doing my dev on a local WAMP server, but the test and production environments are Linux.
I've found several answers on here, some which suggest using pcntl_fork(), others suggesting running a command-line PHP script and others suggesting Ajax.
My problems with each of these approaches:
The pcntl_fork() spins up another instance of apache, which isn't what I want.
Ajax seems to lock the website until the import completes.
I'm trying to run a single FUNCTION rather than an entire script, so I don't believe the command-line will work.
Anyone have any ideas how to run this exclusively in the background? Also, any way to check and see if the process is complete?

Related

How can I run a daemon on xampp using PHP?

I have an XML database that I want to manage independently from users on my website. Looking into the matter it appears that I should write a daemon script to manage my database. That is all fine and dandy but, I feel like I'm opening a can of worms. I wanted to write my daemon script in PHP, so I looked into PCNTL. But I quickly learned that PCNTL is not suited for web servers. So now I am stumped. How can I get a daemon to run on my server? Do I need to learn another language? I only want to write my own scripts. But I feel lost. I would prefer to write my daemon in PHP as I am familiar with the language.
I have been researching everything from PCNTL, CLI, SO questions, numerous articles on daemon processes... etc
I am running PHP 5.6.32 (cli), windows 7, on Apache. XAMPP 5.6.32. Unix system.
EDIT: I also have windows setup to run PHP from command prompt.
There's nothing wrong in running a PHP daemon, however it's not the fastest thing, especially before the 7.0 version. You can proceed in two ways:
Using Cron Jobs, if you're under Unix systems crontab will be fine, in this way you can specify the interval within the system automatically executes the specified script and then exit.
The true daemon, firstly you need to change the max_execution_time in PHP.ini to 0 (infinite), then in your daemon call for first function set_time_limit(0);, remember to run it only once. However if there is some failure like a thrown error uncatched the script will exit and you need to open it again manually, and don't try...catch in a while loop because it will probably go into an endless loop. Execute the script with php -f daemon.php.

Elixir application runs a PHP script

I'm writing an elixir application that needs to communicate with several APIs.
One api is a connection to Post Affiliate Pro which uses PHP and has a PHP class file you can download to use their API easily.
As I seach for answer online my results are overwhelmed about using PHP to run elixir which is the opposite of wha. I need
I know nothing about PHP, I don't even know what dependencies I'll need on the server to run a php script.
My elixir program currently runs an executable using System.cmd().
Would it be possible to use System.cmd() to run the script file and have the output of the PHP script be routed to elixir's STDIN? I've never done that before but maybe I should start there? Any suggestions?
If its a script designed to be run from the command line, the you should be able to run it with System.cmd/1. First try to run the script manually from the command line and get it working.
I would start with ensuring that you have php installed on your system. If not, you will need to install it.
The other option is to use the php script to understand the API its using and create your own in Elixir, using a http client.

Laravel 5.3 scheduler Runs once on Windows then ends without any further processing

OS: Windows 8.1
Laravel-version: 5.3.15
Hello,
I'm having a bit of trouble getting Laravel's scheduler to properly run on on my machine and STAY RUNNING in a way where I can see some sort of output to indicate that it's actually working. Every time I run php artisan schedule:run it calls any listed commands once and then just dies. It doesn't repeat or anything. Example attempt below:
The Code:
The Result When Trying to Run the Scheduler:
What I've tried:
I've tried multiple suggested fixes from the link below and some are actually referenced throughout stackoverflow as well. Including adding a batch file, setting php's path in my system veriables etc etc. as suggested in the link below by Kryptonit3:
https://laracasts.com/discuss/channels/general-discussion/running-schedulerun-on-windows
However, nothing works. Even in the "Windows Task Scheduler" tool it will just run once, end any further processing in the CLI and then close the window. There is no actual indication that this scheduler is running in the background indefinitely (Sort-of like a background job only without the queues).
Questions:
1 - With no indication (the echoed "I am Scheduled") how do I know if this is actually working?
2 - Why does attempting to run this command die after it successfully completes once.
3 - Does my Laravel Version have anything to do the scheduler not running permenently when I try using php artisan schedule:run in the CLI or windows task scheduler?
4 - Mainly what I'm trying to accomplish here is every minute the system would scan a single DB table's field. How do I accomplish this?
5 - Can someone please give me some clarification on this? Or point me in the right direction so that I can get this thing really running indefinitely without it just ending after the first run?
Note: I do not want to use Laravel Forge or any other external service for something so simple. That would be overkill and unecessary I feel.
after doing some rather serious digging I found that all of what Kryptonit3 had said in his step by step answer that I referenced was correct. However, I did not know I needed to restart my computer after creating a new task with the Windows Task Scheduler. I thought this would happen automatically. Would've saved me 2 days of debugging had I'd of known but regardless, if someone else comes across this similar problem they will know to reboot the computer.
Reference:
https://laracasts.com/discuss/channels/general-discussion/running-schedulerun-on-windows

Python, MySQL, MAMP - How to do it?

The short version of the story is that when I import MySQLdb in a script that I run from the browser, I always get 500 Internal Server Error.
The longer version:
Trying to learn web programming concepts. So far I've done a lot of work with PHP and MySQL using MAMP. I have created a big Javascript/PHP app and a big MySQL db to go with it. Now I'd like to learn Python, and I'd like to start with a familiar environment, namely, my MAMP setup and the db I've created.
I run my PHP scripts by entering localhost:8888/the-script.php in my browser. Now I have python scripts that run the same way, so I know python is working in my MAMP environment. What I haven't figured out yet is how to get my python scripts to talk to the MySQL db.
I installed Django, and I got it to see my db, but it wants to run its own server. I don't want that. I want my python scripts to run on the same server as my PHP scripts (I suppose I could try to get my PHP scripts to run on the Django server, but I'd rather consider that a last resort, to be taken only in desperation).
I've tried installing various packages with pip and macports, and I can run python from the command line and import MySQLdb, and it works fine. I've been all over stackoverflow and tried many of the applicable suggestions. I found a straightforward set of installation instructions that I thought might be the answer. But if I import MySQLdb in a script that I run from the browser, I always get 500 Internal Server Error.
I was hoping to learn python without first having to get a PhD in a lot of other areas like how Apache works internally, rebuilding MAMP and MySQL from the source code, etc. I realize that learning web programming concepts will eventually include those things, but I'm not ready for that. Right now I just want to learn python.
Is there a simple way to get a python script to run in the same environment as my PHP scripts, and interact with the same MySQL db that I've created for use with my PHP scripts?

changing a local registry key with PHP

I'm creating a PHP script in other to automate a browser performance test. One of the key features is to be able to enable / disable a plugin in Internet Explorer or Firefox.
Reading about it online, I saw I have to execute a similar command to :
[HKLM\Software\Microsoft\Internet Explorer\Extensions\{6D53EC84-6AAE-4787-AEEE-F4628F01010C}]
“Flags”=dword:00000001
In otehr to accomplish what I need. However, my question is: how can I accomplish that through PHP? I thought about using exec('cmd') but I'm not sure whether that's the correct way.
Does someone have done that before? The script is local only, and always going to work on Windows environment.
Thank you
Obviously, you can only do this running a PHP script locally on a Windows machine, it is not possible* to modify the Windows Registry of some other machine that just sends a request to a PHP script on your server.
But you should be able to run exec in order to change the local machine, some quick googling returned how to delete and add registry keys from the command line which seems promising.
Something like:
<?php
// Write the value you want to save to a .reg file
exec('reg import tweakExtension.reg');
?>
Ought to work. A little more instruction on using the reg command can be gleaned by running reg /?.
* we hope...

Categories