PHP OOP Simple cURL Class Example
A simple PHP cURL Class written in OOP
In this post we will be creating an OOP Version of a cURL Request.
We will be adapting the request methods that we have on this post.
Firstly we will think of a structure of the Class.
class curl{
// class variable that will hold the curl request handler
private $handler = null;
// class variable that will hold the url
private $url = '';
// class variable that will hold the info of our request
public $info = [];
// class variable that will hold the data inputs of our request
private $data = [];
// class variable that will tell us what type of request method to use (defaults to get)
private $method = 'get';
// class variable that will hold the response of the request in string
public $content = '';
// function to set url
public function url( $url = '' ){
}
// function to set data inputs to send
public function data( $data = [] ){
}
// function to set request method (defaults to get)
public function method( $method = 'get' ){
}
// function that will send our request
public function send(){
}
// function that will close the connection of the curl handler
public function close(){
}
}
Now that we have a skeleton of our class.
Let's fill in the codes in each of the functions
// function to set url
public function url( $url = '' ){
$this->url = $url;
return $this;
}
Sets the URL and returns the current class. Note the return $this, this is to say that it will return the current class.
What I want to do is chain these functions into a 1 liner call. It can also be not 1 liner depends on how you want to use it.
For example,
$curl = new curl();
$curl->url('http://example.com')->method('post');
Since our url() function will return the current class, the above is also the same with.
$curl = new curl();
$curl->url('http://example.com');
$curl->method('post');
This is what they call "chaining".
// function to set data inputs to send
public function data( $data = [] ){
$this->data = $data;
return $this;
}
Sets the Method to use in the request and returns the current class.
// function that will send our request
public function send(){
try{
if( $this->handler == null ){
$this->handler = curl_init( );
}
switch( strtolower( $this->method ) ){
case 'post':
curl_setopt_array ( $this->handler , [
CURLOPT_URL => $this->url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => count($this->data),
CURLOPT_POSTFIELDS => http_build_query($this->data),
] );
break;
case 'put':
curl_setopt_array ( $this->handler , [
CURLOPT_URL => $this->url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => http_build_query($this->data),
] );
break;
case 'delete':
curl_setopt_array ( $this->handler , [
CURLOPT_URL => $this->url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_POSTFIELDS => http_build_query($this->data),
] );
break;
default:
curl_setopt_array ( $this->handler , [
CURLOPT_URL => $this->url,
CURLOPT_RETURNTRANSFER => true,
] );
break;
}
$this->content = curl_exec ( $this->handler );
$this->info = curl_getinfo( $this->handler );
}catch( Exception $e ){
die( $e->getMessage() );
}
}
Sends our request and sets the $content & $info variables.
Note the if( $this->handler !== null ){....}. So I want our class to re-use the current connection and will have another function that will close the connection.
Inside the switch( strtolower( $this->method ) ){....} is a minified version of this post
And the full code of our final OOP Simple cURL class.
class curl{
// class variable that will hold the curl request handler
private $handler = null;
// class variable that will hold the url
private $url = '';
// class variable that will hold the info of our request
private $info = [];
// class variable that will hold the data inputs of our request
private $data = [];
// class variable that will tell us what type of request method to use (defaults to get)
private $method = 'get';
// class variable that will hold the response of the request in string
public $content = '';
// function to set data inputs to send
public function url( $url = '' ){
$this->url = $url;
return $this;
}
// function to set data inputs to send
public function data( $data = [] ){
$this->data = $data;
return $this;
}
// function to set request method (defaults to get)
public function method( $method = 'get' ){
$this->method = $method;
return $this;
}
// function that will send our request
public function send(){
try{
if( $this->handler == null ){
$this->handler = curl_init( );
}
switch( strtolower( $this->method ) ){
case 'post':
curl_setopt_array ( $this->handler , [
CURLOPT_URL => $this->url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => count($this->data),
CURLOPT_POSTFIELDS => http_build_query($this->data),
] );
break;
case 'put':
curl_setopt_array ( $this->handler , [
CURLOPT_URL => $this->url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => http_build_query($this->data),
] );
break;
case 'delete':
curl_setopt_array ( $this->handler , [
CURLOPT_URL => $this->url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_POSTFIELDS => http_build_query($this->data),
] );
break;
default:
curl_setopt_array ( $this->handler , [
CURLOPT_URL => $this->url,
CURLOPT_RETURNTRANSFER => true,
] );
break;
}
$this->content = curl_exec ( $this->handler );
$this->info = curl_getinfo( $this->handler );
}catch( Exception $e ){
die( $e->getMessage() );
}
}
// function that will close the connection of the curl handler
public function close(){
curl_close ( $this->handler );
$this->handler = null;
}
}
How to use:
$curl = new curl();
$curl->url('http://example.com')
->method('post')
->send();
// check status code of our request
if( $curl->info['http_status_code'] == 200 ){
// request is good.
$theContent = $curl->content;
// don't close connection and send another request to a different url
$curl->url('......');
$curl->send();
.....
}
$curl->close();
Replies (6)
I think we need to fix one of the method parameter:
public function method( method = 'get' ) to public function method( $method = 'get' )
'$' is missing for the parameter variable name.
if( $this->handler !== null ){
$this->handler = curl_init( );
}
// class variable that will hold the info of our request
private $info = [];
In the first version you have it public but in the last example it is set to private. Due to private setting the check status of $curl->info will not work right? It took me some time to figure out what was wrong as I'm new to OOP.
$curl->info['http_status_code'] == 200 should be $curl->info['http_code'] == 200
Reply