Shardj
Shardj

Reputation: 1969

How to obtain a Reddit feed using PHP's cURL?

I'm trying to get a feed from my Reddit account using PHP's cURL. As a result I need to keep my cookies, currently I'm attempting to do this using a cookies.txt file in Netscape format, but these aren't being applied to the page. I think I must be doing something foolish.

End result I hope to get the page to auto login to Reddit for me before displaying, but I hope to learn before I jump to more complex things.

<?php       
    //format cookies.txt file by adding semi colons to the end of new lines if they aren't already there and removing colons added to #'s
    $cookies = file_get_contents('.\cookies.txt');
    $cookies = str_replace("\n",";\n",$cookies);
    $cookies = str_replace("#;","#",$cookies);
    $cookies = str_replace(";;",";",$cookies);
    file_put_contents("./cookies.txt", $cookies);

    //set user agent and url + initiate
    $user_agent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36";
    $url = 'http://www.reddit.com';
    $c = curl_init($url);

    //My options
    $options = array(
        CURLOPT_CUSTOMREQUEST  =>"GET",        //set request type post or get
        CURLOPT_POST           =>false,        //set to GET
        CURLOPT_USERAGENT      => $user_agent, //set user agent
        CURLOPT_COOKIEFILE     =>".\cookies.txt", //set cookie file
        CURLOPT_COOKIEJAR      =>".\cookies.txt", //set cookie jar
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );
    curl_setopt_array( $c, $options );
    //curl_setopt(... other options you want...)

    $html = curl_exec($c);

    if (curl_error($c))
        die(curl_error($c));

    // Get the status code
    $status = curl_getinfo($c, CURLINFO_HTTP_CODE);

    curl_close($c);

    //get html
    echo $html;
?>

Upvotes: 0

Views: 616

Answers (2)

aholmes
aholmes

Reputation: 458

There are a few issues here. Let's tackle them one at a time.

First, you state that you want this script to log you into reddit. Presumably this is a web-accessible PHP script, and your goal is to set the cookies in your browser when you visit this page.

Problem 1: This may not be possible if the cookie values are specific to the IP address (and possibly other factors) for which the cookie was created. I am not familiar with how reddit handles sessions, so I can't answer this. If the cookie values can be shared amongst any machine then you are good to go.

Problem 2: You aren't sending any headers (including cookies) to your browser, but I suspect you simply haven't gotten to this point yet. When you do get there, you can do this with header() or setcookie().

Second, there are some syntactical problems with your code.

  • You are using \ as the directory separator. This is problematic because a single backslash by itself is the escape character. Although the characters preceded by the \ are not escaped (excluding \n), this is not a good habit to get into.

  • All that said, you should be using DIRECTORY_SEPARATOR instead.

  • You are mixing single-quotes (') and double-quotes("). In PHP this has significance. Read the documentation on strings for more information.

  • You are also mixing forward-slashes (/) and back-slashes (\) for your directory paths.

So now we need to determine a few things, and it would help us debug if you provide your cookie file. Scramble values so we can't hijack your session.

  • What is actually being returned to curl?

  • What is being output to your browser?

  • When were the cookie values generated? It is possible the cookie has expired.

Upvotes: 2

ChickenFarmer
ChickenFarmer

Reputation: 11

cURL can handle the creation and processing of the cookie file on its own. I'd recommend eliminating the code you wrote prior to //set user agent and url + initiate. Next step is to ensure that cookies.txt exists and the user running your script has the appropriate permissions to access the file (760, 770, or 660 should be what you're after if you're running this on Linux).

Upvotes: 0

Related Questions