tags:

views:

57

answers:

2

I have this code:

<?php

 foreach($items as $item) {

   $site = $item['link'];
   $id = $item['id'];
   $newdata = $item['data_a'];
   $newdata2 = $item['data_b'];

   $ch = curl_init($site.'updateme.php?id='.$id.'&data1='.$newdata.'&data2='.$newdata2);
   curl_exec ($ch);
   // do some checking here
   curl_close ($ch);

 }

?>

Sample input:

$site = 'http://www.mysite.com/folder1/folder2/';

$id = 512522;

$newdata = 'Short string here';

$newdata = 'Another short string here with numbers';

Here the main process of updateme.php

if (!$id = intval(Tools::getValue('id')))
    $this->_errors[] = Tools::displayError('Invalid ID!');
else
{
    $history = new History();
    $history->id = $id;
    $history->changeState($newdata1, intval($id));
    $history->id_employee = intval($employee->id_employee);
    $carrier = new Carrier(intval($info->id_carrier), intval($info->id_lang));
    $templateVars = array('{delivery}' => ($history->id_data_state == _READY_TO_SEND AND $info->shipping_number) ? str_replace('@', $info->shipping_number, $carrier->url) : '');
    if (!$history->addWithemail(true, $templateVars))
        $this->_errors[] = Tools::displayError('an error occurred while changing status or was unable to send e-mail to the employee');
}

The site will always be changing and each $items will have atleast 20 data inside it so the foreach loop will run atleast 20 times or more depending on the number of data.

The target site will update it's database with the passed variables, it will probably pass thru atleast 5 functions before it is saved to the DB so it could probably take some time too.

My question is will there be a problem with this approach? Will the script encounter a timeout error while going thru the curl process? How about if the $items data is around 50 or in the hundreds now?

Or is there a better way to do this?

UPDATES: * Added updateme.php main process code. Additional info: updateme.php will also send an email depending on the variables passed.

  • Right now all of the other site are hosted in the same server.
A: 

You can have a php execution time problem.
For your curl timeout problem, you can "fix" it using the option CURLOPT_TIMEOUT.

Spilarix
Any suggestion on how I could run the updateme.php from different site without hitting the PHP execution time problem? Will fopen be any better?
GoDesigner
Since you are on the same server, can't you remove curl and integrate updateme.php in your global script (in the foreach loop) ?
Spilarix
updateme.php is just a process file, the database information is different from each site and the global script doesn't have access to that information. How could I update the database for each site if I do it that way? Or am I reading your suggestion the wrong way?
GoDesigner
I understand... You can increase your php set_time_limit. However, the use of Php to act as a batch is not really a good solution. A desktop application or a script would be better.
Spilarix
A: 

Since the cURL script that calls updateme.php doesn't expect a response, you should make updateme.php return early.

http://gr.php.net/register_shutdown_function

function shutdown() {
    if (!$id = intval(Tools::getValue('id')))
        $this->_errors[] = Tools::displayError('Invalid ID!');
    else
    {
        $history = new History();
        $history->id = $id;
        $history->changeState($newdata1, intval($id));
        $history->id_employee = intval($employee->id_employee);
        $carrier = new Carrier(intval($info->id_carrier), intval($info->id_lang));
        $templateVars = array('{delivery}' => ($history->id_data_state == _READY_TO_SEND AND $info->shipping_number) ? str_replace('@', $info->shipping_number, $carrier->url) : '');
        if (!$history->addWithemail(true, $templateVars))
            $this->_errors[] = Tools::displayError('an error occurred while changing status or was unable to send e-mail to the employee');
    }
}

register_shutdown_function('shutdown');

exit();

You can use set_time_limit(0) (0 means no time limit) to change the timeout of the PHP script execution. CURLOPT_TIMEOUT is the cURL option for setting the timeout, but I think it's unlimited by default, so you don't need to set this option on your handle.

bobdiaes