beatsforthemind
beatsforthemind

Reputation: 877

How would you perform a cURL function on a site after logging into the site using a cURL POST?

I am attempting to login to Reddit using their login API - https://github.com/reddit/reddit/wiki/API%3A-login. I am able to successfully authenticate and store a cookie using

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://www.reddit.com/api/login/USERNAME');
curl_setopt ($ch, CURLOPT_REFERER, "http://www.reddit.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 
curl_setopt($ch, CURLOPT_POST, 1); 
$postData = 'api_type=json&user=USERNAME&passwd=PASSWORD'; 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
curl_exec($ch);

but when I add

curl_setopt($ch, CURLOPT_URL, 'http://www.reddit.com/r/pics');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
echo $data;

to the end of the authentication code I receive the 'page not found' page from Reddit, even though I am succesfully logged in and the page seems to indicate that I am indeed in the /r/pics subreddit. I am wondering if there is some sort of redirect happening or if any options are missing or incorrect.

Upvotes: 0

Views: 82

Answers (1)

Marc B
Marc B

Reputation: 360572

You'd still be performing a POST operation when you load the second URL. CURL will not switch 'back' to a GET unless you tell it to. Try adding in

curl_setopt($ch, CURLOPT_HTTPGET, TRUE);

Upvotes: 1

Related Questions