php - 使用 cURL 和 simple_html_dom 抓取时服务器崩溃
问题描述
我有一个奇怪的问题。我有一个在 localhost 上运行良好的脚本,但是在服务器上运行它时,它会在几个循环后崩溃。该脚本使用cURL
和simple_html_dom
抓取网页。
这是代码的总和:
class updateController extends Controller{
function __construct(){
ini_set('max_execution_time', 0);
set_time_limit(0);
require_once 'simple_html_dom.php';
}
static public function ThemeforestLoopExisting(){
$themes = Fulls::where('X','Y')->get();
foreach($themes as $theme){
$cURL = GeneralFunctions::cURLDom($theme['url']);
//Here I search for specific parts on the web page using the "find" method on simple_html_dom
}
}
}
在GeneralFunctions.php:
static public function cURL_scraping($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A');
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Expect:'));
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($curl, CURLOPT_ENCODING, 'identity');
$response['str'] = curl_exec($curl);
$response['header'] = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
return $response;
}
static public function cURLDom($url){
$cURL_results = generalFunctions::cURL_scraping($url);
$res['header'] = $cURL_results['header'];
$res['str'] = str_get_html($cURL_results['str'],$lowercase=false, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=false, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT);
return $res['str'];
}
整个过程适用于前 10/20/30 运行左右,然后服务器崩溃。它在本地主机上完美运行。我和我的虚拟主机谈过了,但他们没有帮助。
我在这里有什么遗漏或没有意识到的吗?任何帮助将不胜感激......谢谢!
解决方案
这实际上是一个数据库问题。我将排序规则更改为utf8mb4_general_ci
并修复了它。
推荐阅读
- csv - Arduino sscanf 第一次正确解析 CSV 但随后返回垃圾数据或使电路板崩溃
- java - 在Java中获取和修改集合中的对象
- wordpress - 如何停止将外部 http 链接重定向到 WP 页面上的 https
- asp.net - Entity frame work6.0.0.0 中的错误:未使用 Ignore 方法或 NotMappedAttribute 数据注释明确排除类型
- javascript - Javascript map 函数返回空值
- python - 使用 XGBoost 在 PyCharm 上以退出代码 -1073740791 (0xC0000409) 完成的过程
- javascript - 将输入与因子绑定(v-model)
- vb.net - 使用 Char 字典有用吗?
- postgresql - 如何使用 postgresql 查询在表名中添加双引号?
- java - Dagger - MissingBinding 贴图
,提供者 > 如果没有 @Provides-annotated 方法,则无法提供