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 <em>( url NSFW sorry )</em> :
<?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, <strong>$args</strong> );
Who can tell me what is the good <strong>$args</strong> to pass in this function ?
Thank you
<em>ps : please, try your code before posting.</em>
Gabriel Reguly answers:
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: <strong>' . curl_error( $hCURL ) . '</strong>';
$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: <strong>' . curl_error( $hCURL ) . '</strong>';
}
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 );
}
}
}
}
}
?>
Julio Potier comments:
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
Gerard answers:
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.
Julio Potier comments:
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 :)