For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > January 2008 > CURL and PHPBB









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author CURL and PHPBB
zackrspv

2008-01-30, 7:12 pm

Hello there,

My need:

1. Log into PHPbb from a remote server.
2. View topics and forums via the remote server.
3. Post back to a specific topic or forum.
4. Log out.

What I have already done (thanks to a php class file):
1. Log into PHPbb from a remote server.
2. View topics and forums.

What I am having trouble with:
3. Posting and replying to topics from my remote server.

Here is my curl class (i didn't write this) for posting to phpbb:

///////// CODE /////////////
function new_topic($forum_id, $message, $topic_title)
{
global $_SERVER;

// Generate post string
$post_fields = $this->array_to_http(array(
'post' => 'Submit',
'mode' => 'post',
'message' => $message,
'f' => $forum_id,
'subject' => $topic_title,
'disable_bbcode' => 0,
'disable_smilies' => 0,
'attach_sig' => 1,
'topictype' => 0,
));
// Location
$url_vars = $this->array_to_http(array(
'mode' => 'post',
'f' => $forum_id,
));
// Init curl
$this->curl = curl_init();
// Set options
curl_setopt ( $this->curl, CURLOPT_URL, $this->phpbb_url .
'posting.php?' . $url_vars );
curl_setopt ( $this->curl, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ( $this->curl, CURLOPT_POST, true );
curl_setopt ( $this->curl, CURLOPT_POSTFIELDS, $post_fields );
curl_setopt ( $this->curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $this->curl, CURLOPT_HEADER, false );
curl_setopt ( $this->curl, CURLOPT_COOKIE, $this->cookie_name );
curl_setopt ( $this->curl, CURLOPT_COOKIEJAR, $this->cookie_name );
curl_setopt ( $this->curl, CURLOPT_COOKIEFILE, $this->cookie_name );
curl_setopt ( $this->curl, CURLOPT_USERAGENT,
$_SERVER['HTTP_USER_AGENT'] );
// Execute request
$result = curl_exec ( $this->curl );
echo $result;
// Get the result
if ( preg_match ( '#<td align="center"><span class="gen">Your
message has been entered successfully.<br \/><br \/>#is', $result,
$match ) )
{
$post_status = 1;
}
else if ( preg_match ( '#<td align="center"><span class="gen">You
cannot make another post so soon after your last; please try again in
a short while.<\/span><\/td>>#is', $result, $match ) )
{
$post_status = 0;
}
else
{
$post_status = 0;
}
// Error handling
if ( curl_errno ( $this->curl ) )
{
$this->error = array(
curl_errno($this->curl),
curl_error($this->curl),
);
curl_close ( $this->curl );
return false;
}
// Close connection
curl_close ( $this->curl );
// Return result
return $post_status;
}
///////// END CODE ///////////

But, when I send my data to that function, and i echo $result, it says
'You need to be logged in.' But, i have already called the login
routine from my main script.

Here's the login call:

//////// CODE ///////////

// Log in
$phpbb->login('$funame', '$fpass');

//////// END CODE ///////

And, here is the curl login code:

////////// CODE ///////////
function login($username, $password)
{
global $_SERVER;

// Generate post string
$post_fields = $this->array_to_http(array(
'username' => $username,
'password' => $password,
'autologin' => 0,
'redirect' => 'index.php',
'login' => 'Log In',
));
// Init curl
$this->curl = curl_init();
// Set options
curl_setopt ( $this->curl, CURLOPT_URL, $this->phpbb_url . 'ucp.php?
mode=login' );
curl_setopt ( $this->curl, CURLOPT_POST, true );
curl_setopt ( $this->curl, CURLOPT_POSTFIELDS, $post_fields );
curl_setopt ( $this->curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $this->curl, CURLOPT_HEADER, false );
curl_setopt ( $this->curl, CURLOPT_COOKIE, $this->cookie_name );
curl_setopt ( $this->curl, CURLOPT_COOKIEJAR, $this->cookie_name );
curl_setopt ( $this->curl, CURLOPT_COOKIEFILE, $this->cookie_name );
curl_setopt ( $this->curl, CURLOPT_USERAGENT,
$_SERVER['HTTP_USER_AGENT'] );
// Execute request
$result = curl_exec ( $this->curl );
// Error handling
if ( curl_errno ( $this->curl ) )
{
$this->error = array(
curl_errno($this->curl),
curl_error($this->curl),
);
curl_close ( $this->curl );
return false;
}
// Close connection
curl_close ( $this->curl );
// Return result
return true;
}
///////// END CODE ///////////

I think the problem is simply that the class for curl doesn't pull the
session id from the phpbb forum after logging in and pass the session
id with the url variable's when creating the post. I could be wrong,
don't know.

Anyone have any ideas on how to fix/accomplish this? I've been
searching for days without much luck.

I can include the entire class file, if you wish.

Thanks!
deerhack@gmail.com

2008-01-31, 4:22 am

Well ... I using a function by me (deerme)
The which allows me to perform ussing Protocol http (GET o POST) and
Capture de Cookie ...
This a Example.

1.- Login in Forum IPB in aq2chile.cl vi user ZeTTa 311286
2.- Capture Info of Post (http://www.aq2chile.cl/foro/index.php?
showtopic=2738) To which can only be accessed Login in the Forum.


<?
// Login de un Foro IPB
// by deerme.org


function get($host,$puerto,$patch,$referer,$cooki
e)
{
$fp = fsockopen($host,$puerto,$errno, $errstr,3);
fputs($fp,
"GET $patch HTTP/1.1
Host: $host
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:
1.8.0.3) Gecko/20060426 Firefox/1.5.0.3
Accept: text/xml,application/xml,application/xhtml+xml,text/
html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: *
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: $referer
Cookie: $cookie
Connection: Close\r\n\r\n

");

while (!feof($fp))
{
$salida .= fread($fp, 8192);
}
fclose($fp);
return $salida;
}


function post($host,$puerto,$patch,$referer,$cook
ie,$data_lenght,
$data)
{

$fp = fsockopen($host,$puerto,$errno, $errstr,3);
fputs($fp,
"POST $patch HTTP/1.1
Host: $host
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:
1.8.0.3) Gecko/20060426 Firefox/1.5.0.3
Accept: text/xml,application/xml,application/xhtml+xml,text/
html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: *
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: $referer
Cookie: $cookie
Content-Type: application/x-www-form-urlencoded
Content-Length: $data_lenght

$data
Connection: Close\r\n\r\n
");

while (!feof($fp))
{
$salida .= fread($fp, 8192);
}
fclose($fp);
return $salida;



}

$user = "ZeTTa";
$pass = "311286";

$datos = "UserName=".$user."&PassWord=".$pass."&x=23&y=17";

$salida = post("www.aq2chile.cl",80,"/foro/index.php?
act=Login&CODE=01&CookieDate=1","http://www.aq2chile.cl/foro/
index.php?"," session_id=f3325bc0e30a7fa2dc1f2020e04b7
cf2;
ipb_stronghold=c5c4abb621f984b5a72597ea0
bc24369; member_id=0;
pass_hash=0; coppa=0; anonlogin=-1", strlen($datos),$datos );

// Capturamo la Cookie del Servidor
// Creamos Parse para la Cookie
$parser="|Set-Cookie: (.*?)\n|is";
if( preg_match_all($parser, $salida, $capturado) )
{
// Guardamos Cookie
for($i=0;$i<count($capturado[1]);$i++)
{
if ( $i == (count($capturado[1]) -1 ) )
$cookie .= $capturado[1][$i];
else
$cookie .= $capturado[1][$i].'; ';
}
// Eliminamos Enters
$cookie = str_replace("\r","",$cookie);
}


echo $cookie;

// Entramos a la URL para la Cookie
$salida = get("www.aq2chile.cl",80,"/foro/index.php?
showtopic=2738","http://www.aq2chile.uni.cc/foro/index.php?",$cookie);

echo $salida;





//echo "Vamos a Comenzar<br>";
//echo $salida;


?>
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com