热搜:NVER node 开发 php

错误500 ;End of script output before headers: index.php

2024-07-20 22:20:01
错误500 ;End of script output before headers: index.php

ThinkPHP框架,用curl爬取某网站用ajax生成的分页数据,脚本最大执行时间设置为0.运行脚本一段时间后浏览器显示错误500: ,apache错误日志报错:End of script output before headers: index.php。 代码如下:

public function multi(){		$page = 128;//ajax的分页数		$max_size = 10;//并发数		//url数组		$url_arr = array();		//生成url数组		for($i = 1;$i <= $page; $i++){			$url = 'http://www.wdzj.com/front_select-plat?params=&sort=0&currPage=';			$url .= $i;			$url_arr[] = $url;		}		$mh = curl_multi_init();		//每次只放出$max_size大小的url用来初始化		$count = count($url_arr);		for($i=0;$i<$count;$i++){			$ch = curl_init();			curl_setopt($ch, CURLOPT_URL, $url_arr[$i]);			curl_setopt($ch, CURLOPT_HEADER, 0);			curl_setopt($ch, CURLOPT_TIMEOUT, 5);			curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36');			curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);			curl_multi_add_handle($mh, $ch);			$requestMap[] = $ch;			if(count($requestMap)<$max_size && ++$i != $count) continue;//保证数组中每次有$max_size个请求且请求的url还没有用完			$active = null;			//执行批处理句柄			do{				$mrc = curl_multi_exec($mh, $active);			}while($mrc == CURLM_CALL_MULTI_PERFORM);			while($active && $mrc == CURLM_OK){				if(curl_multi_select($mh) != -1){					do{						$mrc = curl_multi_exec($mh, $active);					}while($mrc == CURLM_CALL_MULTI_PERFORM);				}			}						$json_arr = array();//存放ajax返回的接口数据			//获取资源并且关闭句柄			foreach($requestMap as $ch){				$json_arr[] = curl_multi_getcontent($ch);// 				file_put_contents($i.'.txt', $json);				$error = curl_error($ch);				if($error != '') exit('发生了错误:'.$error);				curl_multi_remove_handle($mh, $ch);			}			unset($requestMap);		}		curl_multi_close($mh);		file_put_contents('1.txt', json_encode($json_arr));	}


回复讨论(解决方案)

打开 php 的错误显示,500 多半是被错误信息憋死的

从代码上看,最大的可能是内存不足

你好,平常没有怎么去接触错误显示的设置。 照网上设置,重启apache后。再执行,还是那种错误。我附上apache的完整点的错误日志吧,不过自己看不明白。

[Thu May 05 17:08:52.673028 2016] [core:warn] [pid 20268:tid 320] AH00098: pid file D:/phpStudy/Apache/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?[Thu May 05 17:08:52.811036 2016] [mpm_winnt:notice] [pid 20268:tid 320] AH00455: Apache/2.4.10 (Win32) OpenSSL/1.0.1i mod_fcgid/2.3.9 configured -- resuming normal operations[Thu May 05 17:08:52.811036 2016] [mpm_winnt:notice] [pid 20268:tid 320] AH00456: Apache Lounge VC9 Server built: Jul 19 2014 13:20:51[Thu May 05 17:08:52.811036 2016] [core:notice] [pid 20268:tid 320] AH00094: Command line: 'D:\\phpStudy\\Apache\\bin\\httpd.exe -d D:/phpStudy/Apache'[Thu May 05 17:08:52.814036 2016] [mpm_winnt:notice] [pid 20268:tid 320] AH00418: Parent: Created child process 21132[Thu May 05 17:08:54.476131 2016] [mpm_winnt:notice] [pid 21132:tid 336] AH00354: Child: Starting 150 worker threads.[Thu May 05 17:15:20.696222 2016] [core:error] [pid 21132:tid 1724] [client 127.0.0.1:60080] End of script output before headers: index.php

从代码上看,最大的可能是内存不足



我也一直在看运行过程中的内存,一直没有超过百分之八十


从代码上看,最大的可能是内存不足



我也一直在看运行过程中的内存,一直没有超过百分之八十


我再改下代码吧

系统内存 和 php 可用内存 不是一个概念
你就是个 500,神仙也不知道是怎么回事

php.ini 中令display_errors=On 打开错误提示功能再说吧

开启错误提示

<?phpini_set('display_errors','on');error_reporting(E_ALL);?>

系统内存 和 php 可用内存 不是一个概念
你就是个 500,神仙也不知道是怎么回事


额,是curl_multi_select()函数的问题,删掉那部分就可以了,不过照着参考文档上写的,都有问题,这我就不明白了。内存的不足的问题是在后面出现了,也解决了。多谢额。结贴