Asked  7 Months ago    Answers:  5   Viewed   40 times

I have a PHP script that takes a long time (5-30 minutes) to complete. Just in case it matters, the script is using curl to scrape data from another server. This is the reason it's taking so long; it has to wait for each page to load before processing it and moving to the next.

I want to be able to initiate the script and let it be until it's done, which will set a flag in a database table.

What I need to know is how to be able to end the http request before the script is finished running. Also, is a php script the best way to do this?

 Answers

11

Certainly it can be done with PHP, however you should NOT do this as a background task - the new process has to be dissocated from the process group where it is initiated.

Since people keep giving the same wrong answer to this FAQ, I've written a fuller answer here:

http://symcbean.blogspot.com/2010/02/php-and-long-running-processes.html

From the comments:

The short version is shell_exec('echo /usr/bin/php -q longThing.php | at now'); but the reasons why are a bit long for inclusion here.

Wednesday, March 31, 2021
 
van_folmert
answered 7 Months ago
30

I've never heard of a standard way to "pass" information between PHP and Javascript, as they are a server-side and client-side language, respectively. I would personally use a hybrid of your second and third solutions.

Store the post id in a data-postindex attribute (data attributes are newish, and the "right" way to store small amounts of data). But I would still just use a JSON array for the rest, as storing lots of data in data-attributes (and escaping them!) is potentially problematic. PHP has a json_encode function that takes care of all the escaping and such for you - just build a PHP array (say, $postdata) like you normally would, and then throw this in your post template:

<script type="text/javascript">
    globalPostArray.push(<?php echo json_encode($postdata) ?>);
</script>

Where $postdata is something like the following:

$postdata = array(
    'nid' => 5,
    'authorId' => 45
    ...etc...
);

It should be easy enough to generate such an array from your existing code.

I wrote a blog post a while back about my implementation of this kind of thing, but it sounds like all you need is a pointer at json_encode.

Wednesday, March 31, 2021
 
fardjad
answered 7 Months ago
99

I know it's obvious, but the first place to check is the PHP configuration, make sure that curl is NOT listed in either:

disable_functions=
disable_classes=

While checking php settings, make sure that you have (based on your path):

extension_dir="C:PHPext"

Verify (again) that C:PHPextphp_curl.dll actually exists and that it wasn't just copied from the XAMPP install.

After that, depending on how you launch Apache (as a service using the System account or as a user), you may need to check the user path in addition to the system path.

At one point in time, I'd found that I had multiple copies of ssleay32.dll and libeay32.dll that were being called BEFORE the PHP binaries causing a version conflict. I've included an example of the trouble I ran into below.

To see the full path being used (assuming Apache is being run from user space):

C:> echo %PATH%
C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:Program Filescurl;C:Program Filescurldlls;C:Webphp;C:Python27;

On this machine, these entries are from my user path and not the system path:

C:Program Filescurl;C:Program Filescurldlls;C:Webphp;C:Python27;

To check the location of installed files in the path:

C:> where ssleay32.dll
C:Program Filescurldllsssleay32.dll
C:Webphpssleay32.dll
C:> 

In this example, the included dlls for my builds of curl and php were far enough apart that they were incompatible with each other. PHP was trying to load curl's dlls, but failed. I don't have it installed so I can't check right now, but TortoiseSVN may include them.

It would probably also be worth verifying the permissions on the file:

C:> cacls c:Webphpssleay32.dll
C:Webphpssleay32.dll BUILTINAdministrators:(ID)F
                        NT AUTHORITYSYSTEM:(ID)F
                        BUILTINUsers:(ID)R
                        NT AUTHORITYAuthenticated Users:(ID)C

As a side note, copying files to %SystemDrive%WindowsSystem32 is a bad idea. It's basically just a hack to get whatever files into the system path (in a priority position) without having to explain to the user how to edit the path variables.

Friday, May 28, 2021
 
Freddie
answered 5 Months ago
48

If you really want to use the native php on the system from java, you can try this PHP/Java Bridge. It claims to be faster and more efficient than just shelling out to php. It also appears to be able to call from Java to php or php to java in web and desktop applications. This Java to php example code might be helpful.

Saturday, May 29, 2021
 
Hilmi
answered 5 Months ago
81

Try this to see if SELinux will let the web server connect to the network:

getsebool httpd_can_network_connect

If not, allow it with

setsebool -P httpd_can_network_connect on

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/selinux_users_and_administrators_guide/sect-managing_confined_services-the_apache_http_server-booleans

Thursday, October 21, 2021
 
Jeff
answered 7 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :