Page 1 of 1

Help with simple curl class

Posted: Wed Nov 24, 2010 7:47 pm
by johnferinda
Just starting out with classes and I'm getting the following error:
PHP Notice: Undefined variable: ch on line 33
Line 33:
$this->$ch = curl_init();

Code: Select all

<?php

class Curl
{

	public $ch;
	public $url; 
	public $referer; 
	public $header; 
	public $returnTransfer; 
	public $timeout; 
	public $userAgent; 
	public $output; 

	function __construct($url, $referer, $header, $returnTransfer, $timeout, $userAgent) 
	{
		
		if (!function_exists('curl_init')){ 
			die('CURL is not installed!');
		}
		
		$this->$url = $url; 
		$this->$referer = $referer;
		$this->$header = $header; 
		$this->$returnTransfer = $returnTransfer; 
		$this->$timeout = $timeout; 
		$this->$userAgent = $userAgent; 
		
	}
	
	public function initialize() 
	{
		$this->$ch = curl_init(); 
		 curl_setopt($this->$ch, CURLOPT_URL, $this->$url);
		 curl_setopt($this->$ch, CURLOPT_REFERER, $this->$referer);
		 curl_setopt($this->$ch, CURLOPT_USERAGENT, $this->$userAgent);
		 curl_setopt($this->$ch, CURLOPT_HEADER, $this->$header);
		 curl_setopt($this->$ch, CURLOPT_RETURNTRANSFER, $this->$returnTransfer);
		 curl_setopt($this->$ch, CURLOPT_TIMEOUT, $this->$userAgent);
	}
	
	public function execute()
	{
		$this->$output = curl_exec($this->$ch);	
	}
	
	public function close()
	{
		curl_close($this->$ch);	
	}
	
	public function getOutput()
	{
		return $this->$output;
	}

}

//MAIN

$url = 'http://www.example.com/'; 
$referer = 'Somewhere'; 
$userAgent = 'Chrome';
$header = 0; 
$returnTransfer = 1; 
$timeout = 30; 

$test = new Curl($url, $referer, $header, $returnTransfer, $timeout, $userAgent);
$test->initialize();
$test->execute(); 
$test->close(); 
$page = $test->getOutput(); 
print_r($page); 

?>

Re: Help with simple curl class

Posted: Wed Nov 24, 2010 8:35 pm
by Jonah Bron
It's not $this->$var, the proper syntax is $this->var. You need to correct that throughout your class.

Re: Help with simple curl class

Posted: Wed Nov 24, 2010 9:27 pm
by johnferinda
Jonah Bron wrote:It's not $this->$var, the proper syntax is $this->var. You need to correct that throughout your class.
Can't believe I missed that thanks. :banghead:

Works perfectly:

Code: Select all

<?php

class Curl
{

	public $ch;
	public $url; 
	public $referer; 
	public $header; 
	public $returnTransfer; 
	public $timeout; 
	public $userAgent; 
	public $output; 

	function __construct($url, $referer, $header, $returnTransfer, $timeout, $userAgent) 
	{
		
		if (!function_exists('curl_init')){ 
			die('CURL is not installed!');
		}
		
		$this->url = $url; 
		$this->referer = $referer;
		$this->header = $header; 
		$this->returnTransfer = $returnTransfer; 
		$this->timeout = $timeout; 
		$this->userAgent = $userAgent; 
		
	}
	
	public function initialize() 
	{
		$this->ch = curl_init(); 
		 curl_setopt($this->ch, CURLOPT_URL, $this->url);
		 curl_setopt($this->ch, CURLOPT_REFERER, $this->referer);
		 curl_setopt($this->ch, CURLOPT_USERAGENT, $this->userAgent);
		 curl_setopt($this->ch, CURLOPT_HEADER, $this->header);
		 curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, $this->returnTransfer);
		 curl_setopt($this->ch, CURLOPT_TIMEOUT, $this->userAgent);
	}
	
	public function execute()
	{
		$this->output = curl_exec($this->ch);	
	}
	
	public function close()
	{
		curl_close($this->ch);	
	}
	
	public function getOutput()
	{
		return $this->output;
	}

}

//MAIN

$url = 'http://www.example.com/'; 
$referer = 'Somewhere'; 
$userAgent = 'Chrome';
$header = 0; 
$returnTransfer = 1; 
$timeout = 30; 

$test = new Curl($url, $referer, $header, $returnTransfer, $timeout, $userAgent);
$test->initialize();
$test->execute(); 
$test->close(); 
$page = $test->getOutput(); 
print_r($page); 

?>

Re: Help with simple curl class

Posted: Wed Nov 24, 2010 9:53 pm
by Jonah Bron
Great. Just a few notes. If I were you, I would put close() into execute(), and create individual setter functions for each parameter, instead of having them all in the constructor.

(example)

Code: Select all

class Curl
    ...
    public function setUrl($url)
    {
        curl_setopt($this->ch, CURLOPT_URL, $url);
    }
    
    public function setTimeout($timeout)
    {
        curl_setopt($this->ch, CURLOPT_TIMEOUT, $timeout);
    }
    ...
}

Re: Help with simple curl class

Posted: Wed Nov 24, 2010 10:37 pm
by johnferinda
Jonah Bron wrote:Great. Just a few notes. If I were you, I would put close() into execute(), and create individual setter functions for each parameter, instead of having them all in the constructor.

(example)

Code: Select all

class Curl
    ...
    public function setUrl($url)
    {
        curl_setopt($this->ch, CURLOPT_URL, $url);
    }
    
    public function setTimeout($timeout)
    {
        curl_setopt($this->ch, CURLOPT_TIMEOUT, $timeout);
    }
    ...
}
Great idea. Definitely makes it easier for the user to choose only the ones he needs and for me when I decide to build combo functions (ex: Browser, ftpGet, ftpPut, etc).

Here are the changes:

Code: Select all

<?php

class Curl
{

	public $ch;
	public $url; 
	public $referer; 
	public $header; 
	public $returnTransfer; 
	public $timeOut; 
	public $userAgent; 
	public $output; 

	function __construct($url) 
	{
		
		if (!function_exists('curl_init')){ 
			die('CURL is not installed!');
		}
		
		$this->url = $url; 
		
		print "Starting Curl"; 
		$this->ch = curl_init(); 
		curl_setopt($this->ch, CURLOPT_URL, $this->url);
		
	}
	
	public function setTimeOut($timeOut)
	{
		$this->timeOut = $timeOut; 
		curl_setopt($this->ch, CURLOPT_TIMEOUT, $this->timeOut);
	}
	
	public function setReturnTransfer($returnTransfer)
	{
		$this->returnTransfer = $returnTransfer; 
		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, $this->returnTransfer);
	}
	
	public function setHeader($header)
	{
		$this->header = $header; 
		curl_setopt($this->ch, CURLOPT_HEADER, $this->header);
	}
	
	public function setReferer($referer) 
	{
		$this->referer = $referer; 
		curl_setopt($this->ch, CURLOPT_REFERER, $this->referer);
	}
	
	
	public function setUserAgent($userAgent)
	{
		$this->userAgent = $userAgent; 
		 curl_setopt($this->ch, CURLOPT_USERAGENT, $this->userAgent);
	}
	
	public function execute()
	{
		$this->output = curl_exec($this->ch);	
		return $this->output; 
	}
	
	function close() {
		curl_close($this->ch);
		print "Stopping Curl"; 
	}

}

//MAIN
$test = new Curl('http://www.example.com');
$test->setHeader(1);
$test->setUserAgent('Firefox Curl'); 
$test->setReturnTransfer(1); 
$test->setTimeOut(15); 
$test->setReferer('Somewhere'); 
$page = $test->execute();  
$test->close();

echo '<pre>'; 
print_r($page); 
echo '</pre>'; 

?>
Wondering if I should use a __destruct for the curl close vs the close method.

Re: Help with simple curl class

Posted: Wed Nov 24, 2010 10:41 pm
by Jonah Bron
Good. You can curl_close in execute() after curl_exec. I can't think of a reason why I wouldn't want to close immediately.

Re: Help with simple curl class

Posted: Wed Nov 24, 2010 10:55 pm
by johnferinda
Jonah Bron wrote:Good. You can curl_close in execute() after curl_exec. I can't think of a reason why I wouldn't want to close immediately.
If I wanted to maintain a persistent connection after multiple executes would closing after each execute disrupt it? For example logging into a website and performing multiple actions where the session needs to be maintained.

Under procedural there are times when I need to do the following to maintain cookies/session:

Curl Init
Curl Exec (page1)
Curl Exec (page2)
Curl Exec (page3)
Curl Close

Never sure if it was a bug in curl or if that method was a requirement.

If not I'll definitely put it within the execute to save code.

Thanks for your help BTW Jonah.