Help optimising my curl function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

Help optimising my curl function

Post by spamyboy »

This is re-writen phpbb class, now I'm trying to optimise it, becouse page load time is more than ~30 sec.
What could I change in code to make it faster.

Code: Select all

Code updated & moved lower
Last edited by spamyboy on Tue Jul 24, 2007 3:09 pm, edited 4 times in total.
programmingjeff
Forum Commoner
Posts: 26
Joined: Fri Jan 05, 2007 10:56 am

Post by programmingjeff »

Seeing how slow the Warez-BB.org servers are, I'm not sure there's much you can do. You are trying to make 2 connections before your pageload.

If you want to request multiple pages on the website, then you should login once (in order to initiate the cookie). After that, you only need a single CURL call to request subsequent pages.


I would also recommend changing your password. You don't know who has read this thread and may abuse your account.
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

Post by spamyboy »

Here, I fixed some things.
But still... havent no one got any other sugestions ?

Code: Select all

<?php
class phpbb
{
var $curl=null;
// phpbb function
function phpbb($phpbb_url, $cookie_name = 'tmpfile.tmp')
	{
		if ( empty($phpbb_url) )
		{
			trigger_error('curl_phpbb::error, The phpBB location is required to continue, Please edit your script.');
			return false;
		}
		$this->phpbb_url = $phpbb_url;
		$this->cookie_name = $cookie_name;
}
// end phpbb function

// login function
function login($username, $password)
	{
		global $_SERVER;
		$this->post_fields = $this->array_to_http(array(
			'username'	=> $username,
			'password'	=> $password,
			'autologin'	=> 1,
			'redirect'	=> 'index.php',
			'login'		=> 'Log In',
		));
		$this->open("login.php", TRUE);
		return TRUE;
	}
// end login function

// page open function
function open($phpbb_page, $login)
{
global $_SERVER;
$this->curl = curl_init();
if($login==TRUE) {
curl_setopt ( $this->curl, CURLOPT_POST, true );
curl_setopt ( $this->curl, CURLOPT_POSTFIELDS, $this->post_fields );
} else {
curl_setopt ( $this->curl, CURLOPT_POST, false );
}
curl_setopt($this->curl, CURLOPT_URL, $this->phpbb_url.$phpbb_page);
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'] );
$result = curl_exec ( $this->curl );
curl_close($this->curl);
return $result;
}
// end - page open function
// array to http function
function array_to_http($array)
	{
		$retvar = '';
		//foreach ($array as $field => $data)
		while ( list ( $field, $data ) = @each ( $array ) )
		{
			$retvar .= ( empty($retvar) ) ? '' : '&';
			$retvar .= urlencode($field) . '=' . urlencode($data); 
		}
		return $retvar;
	}
// and array to http function
}

$phpbb=new phpbb("http://www.example.org/");
$phpbb->login("example", "example");
echo $phpbb->open("index.php", FALSE);
?>
Last edited by spamyboy on Tue Jul 24, 2007 3:19 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I'm not removing your password again..
User avatar
spamyboy
Forum Contributor
Posts: 266
Joined: Sun Nov 06, 2005 11:29 am
Location: Lithuania, vilnius

Post by spamyboy »

Sorry for that :)
Post Reply