心血来潮,最近在电驴上下载高清资源---欧美高清MV-720P(http://www.verycd.com/files/fcc77802a50dcea22166f1f7141bff9048612903)。这个页面中右下角如下:
有很多类似的链接。现在就是要把这所有的ed2k链接都保存下来,然后批量下载,而不用一个一个点击下载。
由于上面的链接地址:http://www.verycd.com/files/fcc77802a50dcea22166f1f7141bff9048612903对应如下:
实际上就是对应的是:
。
现在要做的就是取回相关文件下的除了灰色部分的所有链接对应的页面内容,再从中取出相应的ed2k链接。
运行环境如下:
OS:Win7 Professional 64bit
Zend Studio 8.0.1
Zend Server Communication Edition: 5.5.0
代码如下:
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Input the initial url</title>
</head>
<body>
<form action="main.php" method="post" target="_blank">
Please input initial url:<br>
<font color="red">Example:http://www.verycd.com/files/7497c692fc812558e5ba0fc78d84fbf8310530048</font><br>
<textarea rows="10" cols="80" id="initial_url" name="initial_url"></textarea>
<br>
<br>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
main.php
<?php
ob_start();
set_time_limit(0);
$url = $_POST["initial_url"];
//$url = "http://www.verycd.com/files/fb07beed85d207aa756abc998afecc9693668604";
echo "Original url:<br>".$url."<br>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$content_fetched = curl_exec($ch);
if(false == $content_fetched)
{
echo "Sorry, the request is error! Please try again!<br>";
exit;
}
curl_close($ch);
$pattern = '#<a href="(http://www\.verycd\.com/files/[0-9A-Za-z]+)"#i';
preg_match_all($pattern, $content_fetched, $matches);
$ed2k_abstracted = array();
echo "Abstracted ed2k links as follows:<br>";
$all_ed2k_links = "";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
for($i = 0; $i < count($matches[1]); $i++)
{
curl_setopt($ch, CURLOPT_URL, $matches[1][$i]);
$content_fetched = curl_exec($ch);
/*
* <input type="button" class="downloadButton"
* name="ed2k://|file|%5B%E8%BF%88%E5%85%8B%E5%B0%94%E6%9D%
* B0%E5%85%8B%E9%80%8A%E7%8F%8D%E8%B4%B5%E7%BD%95%E8%A7%81
* %E8%A7%86%E9%A2%91%E5%90%88%E9%9B%86%5D.Michael.Jackson.
* -.%5B01.American.Music.Awards.Dangerous.1993%5D.mv.mpg|3
* 10530048|7497c692fc812558e5ba0fc78d84fbf8|h=XMDFNICTGCNK
* X7AXRHTURXVBBAWXSRZF|/" id="downloadButton"
* value="点击下载" />
*/
$pattern = '#ed2k[^"]+#i';
preg_match($pattern, $content_fetched, $match);
$ed2k_abstracted[] = $match[0];
//Display them in the screen.
echo "<font color=red>".$i.":</font>".$match[0]."<br>";
$all_ed2k_links = $all_ed2k_links . $match[0]."\n";
}
curl_close($ch);
ob_end_flush();
/*
* Save as ed2k_abstracted.txt.
*/
file_put_contents("ed2k_abstracted.txt", $all_ed2k_links, FILE_APPEND);
然后出现的很奇怪的问题:
首先碰到了php运行超时的问题。调试的时候一点儿问题都没有,运行的时候就是显示不出来结果。后来在main.php中增加set_time_limit(0);语句,结果还是不对,总是弹出一个main.php的对话框。查找各种日志文件后,在C:\Program Files (x86)\Zend\ZendServer\logs\ZendEnabler.log发现以下错误:
Request timed out during the communication with the PHP process。
Google之,终于在:http://kb.zend.com/index.php?View=entry&EntryID=476找到了答案。
重要摘抄如下:
A Server Error Appears During a Long Debug Session
|
This article applies to:
[ Zend Server x.x, Zend Studio x.x ]
[ Windows ]
Symptom
An error message appears when remotely debugging a PHP application running on a Windows server. Depending on the Web server, the error is either the standard HTTP Error 500 or the Open/Save dialog prompting you to save an empty file:
Technical Details
This problem happens only on Windows with Zend Server installed and the PHP code is handled by FastCGI on this platform. The available FastCGI implementations are very limited in their ability to communicate with the Web server on one side, and with the PHP-CGI process on the other side. Therefore, during a long debug session the FastCGI process may reach its timeout while waiting for PHP to complete the debug session. The FastCGI may then return an error to the Web server even though the PHP process is still working.
Resolution / Workaround
To workaround this limitation, the user can increase the timeout value to a reasonably high number, thus eliminating the possibility that a timeout will occur. The instructions for all supported Web servers are given below. In all cases, restart the Web server after making the change.
Apache
If Zend Server is installed with the bundled Apache Web server, it uses the FastCGI component called Zend Enabler. Edit the configuration file of Zend Enabler as it appears in the below Listing (the timeout value is shown in bold):
<ZendEnablerConfiguration version="2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ZendEnablerConf.xsd">
<Global>
<LogFile path="C:\Program Files\Zend\ZendServer\logs\ZendEnabler.log"
verbosity="0" maxFileSizeKB="100" />
</Global>
<DefaultPool impersonation="true">
<Environment>
<Variable name="DB2INSTANCE" value="DB2" />
<Variable name="PHP_FCGI_MAX_REQUESTS" value="10000" />
<Variable name="TEMP" value="C:\DOCUME~1\user\LOCALS~1\Temp" />
<Variable name="PHPRC" value="C:\Program Files\Zend\ZendServer\etc" />
<Variable name="PATH"
value="C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Zend\MySQL51\bin;C:\Program Files\Zend\ZendServer\bin" />
</Environment>
<ProcessCount start="2" extra="8" />
<Timeouts connectionTimeout="60" requestTimeout="1200" />
</DefaultPool>
<ServerPool mimeType="application/x-httpd-php"
commandLine="C:\Program Files\Zend\ZendServer\bin\php-cgi.exe"
impersonation="true">
</ServerPool>
</ZendEnablerConfiguration>
It works! Great!