几个月前写的通过PHP获得iTunes Store的封面图因为最近iTunes Store调整的原因不能用了,不过还好改动不算太大,代码只需要改一小部分就可以了。
Code
<?php print"Input iTunes URL text file:"; $path = @fgets(fopen('php://stdin', 'r'), 1000) or die('Can not open stdin.'); $input = fopen(trim($path), 'r'); # 获取储存了网页版地址的文件 # e.g /Users/YOUR_USERNAME/Desktop/Music/1.txt print"Input cover save folder path:"; $coverPath = @fgets(fopen('php://stdin', 'r'), 1000) or die('Can not open stdin.'); # e.g /Users/YOUR_USERNAME/Desktop/Music/ if (isset($coverPath)) { $coverPath = trim($coverPath); if (substr($coverPath,-1) != '/') { $coverPath .= '/'; } } # 确保这是一个文件夹的路径 $count = 0; while (!feof($input)) { $buffer = strtolower(trim(fgets($input, 4096))); # 去掉无关的字符 if (ereg('^(http|https)://itunes.apple.com/', $buffer) || ereg('^itunes.apple.com/', $buffer)) { # 我们把URL的开头部分和http://itunes.apple.com/或者itunes.apple.com/做比较 # 遵守上面这个模式的字符串是我们需要的 $count++; # 处理的封面数 print "n 正在处理第[$count]个封面n"; print " | 抓取iTunes Store网页...n"; $content = file_get_contents($buffer); # 获取网页内容 if (isset($content)) { print " | 已获得第[$count]个封面的iTunes Store网页内容n"; preg_match('(http://...mzstatic.com[^"]*(170|255|326)x(170|255|326)(-75|).jp(e|)g)', $content, $match); # 第一次正则提取,得到网页版缩略图地址 # 上面得到的类似于http://a3.mzstatic.com/us/r30/Music/f7/ec/63/mzi.yclyevyf.255x255-75. $string = $match[2]."x".$match[2]; $coverURL = str_replace($string,"1200x1200",$match[0]); # 第二次用正则替换,将255x255或者170x170的缩略图换成1200x1200的大图 print " | 正在下载第[$count]个的封面n"; ob_start(); readfile($coverURL); $img = ob_get_contents(); ob_end_clean(); $size = strlen($img); # 获取1200x1200的大图 if ($size > 0) { print " | 下载成功!正在保存...n"; $filename = $coverPath.end(split('/',$coverURL)); $fp2 = @fopen($filename, "a"); fwrite($fp2,$img); fclose($fp2); # 保存图片 } else { print " | 处理失败,暂时不能下载第[$count]个封面n"; } } else { print " | 处理失败,暂时不能获取第[$count]个文件的iTunes Store网页内容n"; } } } ?>