Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to scrape images from reddit, having trouble cleaning up strings
    primarykey
    data
    text
    <p>So I'm not asking for you to fix my script, if you know the answer I would appreciate it if you just pointed me in the right direction. This is a script I found and I'm trying to edit it for a project. </p> <p>I believe that whats going on is the formatting of $reddit is causing problems when I input that string into $url. I am not sure how to filter the string. </p> <p>Right after I posted this I had the idea of using concatenation on $reddit to get the desired result instead of filtering the string. Not sure.</p> <p>Thanks!</p> <p><strong>picgrabber.php</strong></p> <pre><code>include("RIS.php"); $reddit = "pics/top/?sort=top&amp;t=all"; $pages = 5; $t = new RIS($reddit, $pages); $t-&gt;getImagesOnPage(); $t-&gt;saveImage(); </code></pre> <p><strong>RIS.php</strong></p> <pre><code>class RIS { var $after = ""; var $reddit = ""; public function __construct($reddit, $pages) { $this-&gt;reddit = preg_replace('/[^A-Za-z0-9\-]/', '' , $reddit); if(!file_exists($this-&gt;reddit)) { mkdir($this-&gt;reddit, 0755); } $pCounter = 1; while($pCounter &lt;= $pages) { $url = "http://reddit.com/r/$reddit/.json?limit=100&amp;after=$this-&gt;after"; $this-&gt;getImagesOnPage($url); $pCounter++; } } private function getImagesOnPage($url) { $json = file_get_contents($url); $js = json_decode($json); foreach($js-&gt;data-&gt;children as $n) { if(preg_match('(jpg$|gif$|png$)', $n-&gt;data-&gt;url, $match)) { echo $n-&gt;data-&gt;url."\n"; $this-&gt;saveImage($n-&gt;data-&gt;url); } $this-&gt;after = $js-&gt;data-&gt;after; } } private function saveImage($url) { $imgName = explode("/", $url); $img = file_get_contents($url); //if the file doesnt already exist... if(!file_exists($this-&gt;reddit."/".$imgName[(count($imgName)-1)])) { file_put_contents($this-&gt;reddit."/".$imgName[(count($imgName)-1)], $img); } } } </code></pre> <p>Notice: Trying to get property of non-object in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\RIS.php on line 33</p> <p>Warning: Invalid argument supplied for foreach() in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\RIS.php on line 33</p> <p>Fatal error: Call to private method RIS::getImagesOnPage() from context '' in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\vollyeballgrabber.php on line 23</p> <p><strong>line 33:</strong></p> <pre><code> foreach($js-&gt;data-&gt;children as $n) { var_dump($url); </code></pre> <p><strong>returns:</strong></p> <pre><code>string(78) "http://reddit.com/r/pics/top/?sort=top&amp;t=all/.json?limit=100&amp;after=" NULL </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload