1

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.
2
  • This is almost certainly an inefficient way of doing this. Will all the curl requests be to different servers, or will they all be to the same server? If so, can you edit the code to updateme.php? Commented Oct 6, 2010 at 13:39
  • Right now all curl requests will be in the same server.
    – GoDesigner
    Commented Oct 6, 2010 at 14:02

2 Answers 2

0

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

4
  • 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
    Commented Oct 6, 2010 at 14:03
  • 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
    Commented Oct 6, 2010 at 14:21
  • 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
    Commented Oct 6, 2010 at 14:43
  • 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
    Commented Oct 7, 2010 at 8:46
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.