PHP';s的cURL对象有一个超时选项,可以自动停止脚本


does PHP's cURL object have a timeout option that automaticly stop the script?

我有一个通过corn作业运行的小PHP脚本。这个脚本从数据库中读取记录,然后使用PHP的SimpleXMLElement对象创建一个.xml字符串。最后我对API进行了一个cURL调用,并将文件传输过来。

我遇到的问题是,如果数据库查询的记录比平时多得多,那么脚本似乎不会贯穿所有记录。似乎cURL执行了一个break;,它将阻止foreach循环遍历所有记录。

代码类似于这个

$results = array(........);
foreach($result AS $r){
$xml = new SimpleXMLElement();
 $UpFile =  $xml->addChild('UpFile');
...
...
...
        $ch = curl_init($url);
        curl_setopt_array($ch, $options);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array('xml' => $xml->asXML())  );
        $result = curl_exec($ch);
        $result_xml = simplexml_load_string( $result);
        if (!$result){
            echo curl_error($ch) . '<br />';
            return false;
        }
        curl_close($ch);
}

我想知道cURL对象是否有自动中断foreach循环的限制?如果没有任何线索的话,是什么导致了这个问题?

感谢

首先,当您没有得到$result时,不要返回false。

第二,你可以像这个一样增加超时

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); 
curl_setopt($ch, CURLOPT_TIMEOUT, 400); //timeout in seconds - the default is 30, I believe

此外,在脚本的顶部

set_time_limit(0);