I am pretty new to Php and I try to write some code that will scrape images from reddit.
Right off the bat I have a problem. When I execute this code reddit wants to confirm that I am over 18, which makes sense because its a NSFW subreddit. Not sure where to go from here.
Any input is appreciated, I am not looking for someone to copy/paste the required code I would like to learn, thank you!
The reason I am trying to login is so I can validate that I am over 18 automatically through my session.
<?php
$ch = curl_init();
$data = array('user' => 'xxxx', 'passwd' => 'xxxx', 'rem' => TRUE);
curl_setopt($ch, CURLOPT_URL, 'http://www.reddit.com/api/login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'Cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'Cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_exec($ch);
$url = 'http://reddit.com/r/ass';
$page = file_get_contents($url);
var_dump($page);
?>
If you want to retrieve a list of posts for a subreddit, you’ll be better off by retrieving the list of posts formatted as JSON instead of the regular HTML. Postfix the URL to the subreddit with .json and decode it to PHP structures by using json_decode(). For an added bonus you won’t need to confirm you’re over 18 to retrieve the content.
Example
This doesn’t answer your question, but it solves your problem.