昨日,在项目中,有人在判断Youtube下的某个视频是否能够播放,写下了如下的代码:
$request_url = 'http://youtube.com/get_video_info?video_id=' . $vid . '&el=vevo&fmt=18&asv=2&hd=1'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $request_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, True); $data = curl_exec($ch); curl_close($ch); unset($ch); if ($data === false) { $this->deleteVideo($video['id']); echo "节目: " . $vid . " 节目id" . $video['id'] . "无效,删除. "; continue; } parse_str($data, $details); unset($data); if (!isset($details['url_encoded_fmt_stream_map']) || empty($details['url_encoded_fmt_stream_map'])) { $this->deleteVideo($video['id']); echo "节目: " . $vid . " 节目id" . $video['id'] . "无效,删除. "; continue; } $newstr = explode(",", $details['url_encoded_fmt_stream_map']); $str1 = explode("&", $newstr[0]); $obj = array(); for ($j = 0; $j < count($str1); $j++) { $str2 = explode("=", $str1[$j]); if (!isset($obj[$str2[0]])) { $obj[$str2[0]] = $str2[1]; } } $ary_re['source_url'] = urldecode($obj['url']); if (empty($ary_re['source_url']) || !isset($ary_re['source_url'])) { $this->deleteVideo($video['id']); echo "节目: " . $vid . " 节目id" . $video['id'] . "无效,删除. "; continue; } else { $bool = $this->getMobileCurl($ary_re['source_url']); //不能够播放 if ($bool === false) { $this->deleteVideo($video['id']); echo "节目: " . $vid . " 节目id" . $video['id'] . "无效,删除. "; continue; } } $right_nums++;
getMobileCurl的函数定义如下:
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $request_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, True); $data = curl_exec($ch); curl_close($ch); unset($ch); if ($data === false) { return false }
使用 curl_exec判断当前连接能否播放,这样就出现了一个问题:
提示内存溢出!
我专门调试了一下,发现$data的数据占的内存很大,打开那个$request_url,也就是Youtube的视频的实际播放地址,才发现,视频是高清1080P,而且是有1个多小时的长度,这才发现问题的原因是$data这是是视频的内容,所以会溢出,
果断将getMobileCurl的函数处理成获取服务器的响应头信息:
function getMobileCurl($url) { $res = get_headers($url, 1); $response_status = $res[0]; if (strpos($response_status, "200") !== false || strpos($response_status, "302") !== false || strpos($response_status, "301") !== false) { return true; } else { return false; } }
问题解决:
在使用Curl函数进行爬虫处理的时候,要注意目标是否是个视频,文件等比较大的目标,以及自己的需求。