Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run php script without output to browser

I have a VERY labor intensive PHP script, which does several api calls to a server elsewhere. I need to run this script to keep certain data on my server, synchronized with data on the remote server.

I want this script to start every time a specific type of user visits a specific page.

My problem is however, if a user that is qualified goes to this page, page load-time is REDONCULOUS, even though the data the script processes, doesn't effect the page itself in any way.

So, what i was wondering is, how can i run this script using the same conditions, but run it only on my server?

In other words, how can i run this script and stop the browser from waiting for its output?

EDIT: useful information: Using XAMPP for Windows, PHP 5.5, Apache 2.4.

EDIT 2: Using curl seems to be the best option, but it doesn't want to actually run my script.

Here's the call:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost/tool/follow/PriceTableUpdate.php');
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);
curl_exec($ch);

And here is the actual PriceTableUpdate.php:

<?php  
ini_set('max_execution_time', 3600);
$marketData=simplexml_load_file("http://www.eve-markets.net/api/xml?listid=20158key=JKgLjgYvlY6nP");
foreach ($marketData->marketList->type as $type) {
     $ItemID = (int)$type['id'];
     $ItemBuy = $type->buy->price;
     $ItemSell = $type->sell->price;
     $ItemMedian = $type->median->price;
     mysqli_query($con,"UPDATE piprices SET `ItemBuyPrice` = $ItemBuy, `ItemSellPrice` = $ItemSell, `ItemMedianPrice` =$ItemMedian WHERE `piprices`.`ItemID` = $ItemID");
 }
?>

EDIT 3: Using the above DOES work, in case anyone ever wants to ask this question again. You have to remember though, that since you are using curl, the php file no longer uses variables you've set before, so you will need to define your database connection in the php file again.

like image 982
Tommy Kaira Avatar asked Jan 12 '23 14:01

Tommy Kaira


1 Answers

Why not using AJAX in this? When the page loads and meets your specific conditions, make an AJAX request to the server and start the script without waiting for a response back to the browser.

like image 98
CristiC Avatar answered Jan 16 '23 20:01

CristiC