logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
Using wp_remote_get with cookies

Hello

I'm trying to remote get a full page using cookies, i tried many many configurations but i can not figure out why this is not working.

Heres my basic code ( url NSFW sorry ) :
<?php
while( !is_file( 'wp-load.php' ) ){
if( is_dir('../') ) chdir( '../' );
else die( 'Could not find WordPress.' );
}
DEFINE( 'SHORT_INIT', true );
require ( 'wp-load.php' );
$url = 'http://www.reddit.com/r/redheads/'; // NSFW
$content = wp_remote_get( $url );
header("Content-Type:text/plain");
var_dump( $content );
?>

This page can not be viewed without a cookie named "over18" containing your session id.
I think i have to add cookies here : wp_remote_get( $url, $args );

Who can tell me what is the good $args to pass in this function ?

Thank you

ps : please, try your code before posting.

This question has been answered.

Julio Potier | 10/18/11 at 1:33am Edit

Previous versions of this question: 10/18/11 at 8:50am

The experts have suggested, on average, a prize of $35 for this question.

(2) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    10/18/11
    9:11am
    Gabriel Reguly says:

    Hi Julio,

    Sorry, but $5 is not worth a lot of time.

    So I'll help you with the basic info and you will have to find yourself the correct values for reddit ;-)


    $url = 'http://www.reddit.com/r/redheads/'; // NSFW

    $cookie = new WP_Http_Cookie( 'over18' );
    $cookie->name = 'over18';
    $cookie->value = '8cfed3ce4e1c0bd7d2763c2839a689b69e5f8d6c';
    $cookie->expires = mktime( 0, 0, 0, date('m'), date('d') + 7, date('Y') ); // expires in 7 days
    $cookie->path = '/';
    $cookie->domain = '.reddit.com';

    $cookies[] = $cookie;

    $cookie2 = new WP_Http_Cookie( 'reddit_first' );
    $cookie2->name = 'reddit_first';
    $cookie2->value = '%7B%22firsttime%22%3A%20%22first%22%7D';
    $cookie2->expires = mktime( 0, 0, 0, date('m'), date('d') + 7, date('Y') );
    $cookie2->path = '/';
    $cookie2->domain = '.reddit.com';

    $cookies[] = $cookie2;

    $args = array( 'cookies' => $cookies );

    $content = wp_remote_get( $url, $args );



    Good luck,
    Gabriel


    Edit: Thanks for the prize money.

    If I knew you accepted a cURL solution, I would have shared mine before ( it does a login, then loads a page )


    <?php
    if ( ! function_exists( curl_init ) ) {
    ?>
    <p class="erro"> Não é possível consultar site ! <br />
    Falta função curl_init no PHP. </p>
    <p><a href="http://example.com/">Extranet</a></p>
    <?php
    } else {
    $hCURL = @curl_init(); // create cURL handle ( )
    if ( ! $hCURL ) {
    ?>
    <p>Couldn't initialize a cURL handle.</p>
    <?php
    } else {
    $bError = false;
    $sCookieJar = '/tmp/cookies';
    $sReferer = 'http://example.com/index.htm';
    $sURL = 'http://example.com/cgi-bin/login.plx';
    $sPost = 'login=' . urlencode( $_REQUEST['login'] ) . '&senha=' . urlencode( $_REQUEST['senha'] );
    curl_setopt( $hCURL, CURLOPT_FAILONERROR, 1 );
    curl_setopt( $hCURL, CURLOPT_HEADER, 0 );
    curl_setopt( $hCURL, CURLOPT_RETURNTRANSFER, 0 );
    curl_setopt( $hCURL, CURLOPT_TIMEOUT, 30 );
    curl_setopt( $hCURL, CURLOPT_VERBOSE, 0 );
    curl_setopt( $hCURL, CURLOPT_FOLLOWLOCATION, 0 );
    curl_setopt( $hCURL, CURLOPT_COOKIEJAR, $sCookieJar );
    curl_setopt( $hCURL, CURLOPT_POST, 1 );
    curl_setopt( $hCURL, CURLOPT_POSTFIELDS, $sPost );
    curl_setopt( $hCURL, CURLOPT_URL, $sURL );
    curl_setopt( $hCURL, CURLOPT_REFERER, $sReferer );
    ob_start();
    $sReturn = curl_exec( $hCURL );
    ob_end_clean();
    if ( curl_errno( $hCURL ) ) {
    echo 'cURL error: ' . curl_error( $hCURL ) . '';
    $bError = true;
    }
    curl_close( $hCURL );
    if ( ! $bError ) {
    $hCURL = curl_init(); // create cURL handle ( )
    if ( ! $hCURL ) {
    ?>
    <p>Couldn't initialize a second cURL handle.</p>
    <?php
    } else {
    $sReturn = '';
    $sReferer = 'http://example.com/cgi-bin/login.plx';
    $sURL = 'http://example.com/cgi-bin/monta_layout.plx?entrada=1';
    curl_setopt( $hCURL, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt( $hCURL, CURLOPT_COOKIEFILE, $sCookieJar );
    curl_setopt( $hCURL, CURLOPT_URL, $sURL );
    curl_setopt( $hCURL, CURLOPT_REFERER, $sReferer );
    if ( false === ( $sReturn = curl_exec( $hCURL ) ) ) {
    echo 'cURL error after login: ' . curl_error( $hCURL ) . '';
    }
    curl_close( $hCURL );
    if ( strstr( $sReturn, '<meta http-equiv="refresh" content="0;URL=/cgi-bin/relogin.plx?script=monta_layout.plx">' ) ) {
    echo 'Login inválido';
    } else {
    echo ( $sReturn );
    }
    }
    }
    }
    }
    ?>


    Previous versions of this answer: 10/18/11 at 9:11am

    • 10/18/11 8:52am

      Julio Potier says:

      Of course i understand that $5 is low, in fact, i was waiting for someone who already got this kind of code in their files.
      But you can count on me, if i see that someone waste his time for one of my question, the price will be raised ;)

      Thank you dude ! Mines is the man with his CURL function.

      But your function works for a wp_reote_get, so you both win $5 ;)

      Thanks too

  • avatar
    Last edited:
    10/18/11
    7:46am
    Mines says:

    Do not use wp_remote_get as you have to deal with POST requests and some issues with cookies.
    Use curl instead:


    /* cookie file */
    $cookie_file = dirname(__FILE__) . '/cookie.txt';
    /* url we need to access */
    $confirmation_url = 'http://www.reddit.com/r/redheads/over18?dest=%2Fr%2Fredheads';
    /* perform a post request with our cookie. using curl */
    $handle = curl_init();
    curl_setopt($handle, CURLOPT_URL, $confirmation_url);
    curl_setopt($handle, CURLOPT_COOKIEJAR, $cookie_file);
    curl_setopt($handle, CURLOPT_COOKIEFILE, $cookie_file);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($handle, CURLOPT_POST, true );
    curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt($handle, CURLOPT_POSTFIELDS, array('over18' => 'yes', 'uh' => ''));
    /* reponse contains our page */
    $response = curl_exec($handle);


    Note: $response will be your page in HTML.

    • 10/18/11 8:49am

      Julio Potier says:

      Ok i see ... the button is a POST method, of course -_-
      So, mines, your my man. Your code works !
      Thank you !!
      As i can see, you're new here, welcome :)

This question has expired.



Julio Potier voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.