所以我不是要你修复我的脚本,如果你知道答案,如果你指出我正确的方向,我将不胜感激。这是我找到的一个脚本,我正在尝试为一个项目编辑它。
我相信当我将该字符串输入到 $url 时,$reddit 的格式会导致问题。我不确定如何过滤字符串。
在我发布这个之后,我有了在 $reddit 上使用连接来获得所需结果而不是过滤字符串的想法。没有把握。
谢谢!
picgrabber.php
include("RIS.php");
$reddit = "pics/top/?sort=top&t=all";
$pages = 5;
$t = new RIS($reddit, $pages);
$t->getImagesOnPage();
$t->saveImage();
RIS.php
class RIS {
var $after = "";
var $reddit = "";
public function __construct($reddit, $pages) {
$this->reddit = preg_replace('/[^A-Za-z0-9\-]/', '' , $reddit);
if(!file_exists($this->reddit)) {
mkdir($this->reddit, 0755);
}
$pCounter = 1;
while($pCounter <= $pages) {
$url = "http://reddit.com/r/$reddit/.json?limit=100&after=$this->after";
$this->getImagesOnPage($url);
$pCounter++;
}
}
private function getImagesOnPage($url) {
$json = file_get_contents($url);
$js = json_decode($json);
foreach($js->data->children as $n) {
if(preg_match('(jpg$|gif$|png$)', $n->data->url, $match)) {
echo $n->data->url."\n";
$this->saveImage($n->data->url);
}
$this->after = $js->data->after;
}
}
private function saveImage($url) {
$imgName = explode("/", $url);
$img = file_get_contents($url);
//if the file doesnt already exist...
if(!file_exists($this->reddit."/".$imgName[(count($imgName)-1)])) {
file_put_contents($this->reddit."/".$imgName[(count($imgName)-1)], $img);
}
}
}
注意:尝试在第 33 行的 C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\RIS.php 中获取非对象的属性
警告:第 33 行 C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\RIS.php 中为 foreach() 提供的参数无效
致命错误:从第 23 行 C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\vollyeballgrabber.php 中的上下文 '' 调用私有方法 RIS::getImagesOnPage()
第 33 行:
foreach($js->data->children as $n) {
var_dump($url);
返回:
string(78) "http://reddit.com/r/pics/top/?sort=top&t=all/.json?limit=100&after=" NULL