2010-10-06 66 views
1

我有这样的代码:PHP卷曲的问题 - 运行多次

<?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); 

} 

?> 

样品输入:

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

$id = 512522; 

$newdata = 'Short string here'; 

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

这里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'); 
} 

该网站的主要过程将总是在变化,每个$ items中至少有20个数据,所以foreach循环将运行至少20次或更多根据数据的数量。

目标站点将使用传递的变量更新其数据库,它可能会在保存到数据库之前通过至少5个函数,因此它可能也需要一些时间。

我的问题是这种方法会有问题吗?脚本在通过卷发过程时是否会遇到超时错误?如果$ items的数据大约是50,或者现在有几百个呢?

或者是否有更好的方法来做到这一点?

更新: *增加了updateme.php主进程代码。附加信息:updateme.php也会根据传递的变量发送一封电子邮件。

  • 现在所有其他网站都托管在同一台服务器上。
+0

这几乎肯定是这样做的低效方式。所有的curl请求是否都是针对不同的服务器的,或者它们全都是针对同一台服务器的?如果是这样,你能编辑代码到'updateme.php'吗? – lonesomeday 2010-10-06 13:39:15

+0

现在所有的curl请求都将在同一台服务器上。 – GoDesigner 2010-10-06 14:02:35

回答

0

你可以有一个php执行时间的问题。
对于您的卷曲超时问题,您可以使用选项CURLOPT_TIMEOUT“修复”它。

+0

关于如何在不触及PHP执行时间问题的情况下从不同站点运行updateme.php的任何建议?会打开更好吗? – GoDesigner 2010-10-06 14:03:57

+0

既然你在同一台服务器上,你不能在你的全局脚本(在foreach循环中)中删除curl并集成updateme.php吗? – Spilarix 2010-10-06 14:21:23

+0

updateme.php只是一个进程文件,数据库信息与每个站点不同,全局脚本无法访问该信息。如果我这样做,我怎么能更新每个网站的数据库?或者我读错了你的建议? – GoDesigner 2010-10-06 14:43:15

0

由于调用updateme.php的cURL脚本并不期望得到响应,所以应该尽早让updateme.php返回。

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(); 

您可以使用set_time_limit(0)(0表示没有时间限制),以改变PHP脚本执行的超时。 CURLOPT_TIMEOUT是设置超时的cURL选项,但我认为它默认情况下是无限的,所以你不需要在你的手柄上设置这个选项。