i wanted move to OOP, and trying to understand it. So far i made my first class. Here is the Code. (The problem is with the function sCURL() and returning values and access it right.
class CURL {
public $url;
private $header = false; // DISPLAY HEADERS (FALSE OR TRUE)
private $follow = true; // FOLLOW REDIRCETS (FALSE OR TRUE)
private $useragent = "Mozilla/4.0 (compatible; MSIE 6.0;Windows NT 5.1)"; // SET USER AGENT e.g. "Mozilla/4.0 (compatible; MSIE 6.0;Windows NT 5.1)"
private $referer = "http://www.google.com"; // SET REFERER e.g. http://www.google.com
private $ssl = false; // If set false (it accpets any ssl) should false
private $ctimeout = 5; // Timeout for connect in SECs when curl does next url
private $timeout = 60; // Timeout of retriving page in SECs when curl does next url
public function setHeader($header) {
$this->header = $header;
}
public function setFollow($follow) {
$this->follow = $follow;
}
public function setUseragent($useragent) {
$this->useragent = $useragent;
}
public function setReferer($referer) {
$this->referer = $referer;
}
public function setSsl($ssl) {
$this->ssl = $ssl;
}
public function setCtimeout($ctimeout) {
$this->ctimeout = $ctimeout;
}
public function setTimeout($timeout) {
$this->timeout = $timeout;
}
public function __construct($url) {
$this->url = $url;
}
public function sCURL() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_HEADER, $this->header);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $this->useragent);
curl_setopt($ch, CURLOPT_REFERER, $this->referer);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->ssl);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->ctimeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
}
and this are the results:
object(CURL)#1 (9) {
["url"]=>
string(23) "http://www.facebook.com"
["header:private"]=>
bool(false)
["follow:private"]=>
bool(true)
["useragent:private"]=>
string(49) "Mozilla/4.0 (compatible; MSIE 6.0;Windows NT 5.1)"
["referer:private"]=>
string(21) "http://www.google.com"
["ssl:private"]=>
bool(false)
["ctimeout:private"]=>
int(5)
["timeout:private"]=>
int(60)
["data"]=>
NULL
}
as you can see "data" = NULL.
than i replaced this peace of code
$data = curl_exec($ch);
curl_close($ch);
return $data;
with this:
$this->data = curl_exec($ch);
curl_close($ch);
return $this->data;
and now this are the results (Working):
object(CURL)#1 (9) {
["url"]=>
string(23) "http://www.facebook.com"
["header:private"]=>
bool(false)
["follow:private"]=>
bool(true)
["useragent:private"]=>
string(49) "Mozilla/4.0 (compatible; MSIE 6.0;Windows NT 5.1)"
["referer:private"]=>
string(21) "http://www.google.com"
["ssl:private"]=>
bool(false)
["ctimeout:private"]=>
int(5)
["timeout:private"]=>
int(60)
["data"]=>
string(33320) "<!DOCTYPE html>.........STRIPPED OUTBUT THATS WHAT I WANTED........"
Ok so this is how i call the class
$data1 = new Curl("http://www.facebook.com");
$data1->sCURL();
var_dump($data1);
this gives me the above results. Here is my problem i want access only the "DATA" thing.
$data1 = new Curl("http://www.facebook.com");
$data1->sCURL();
var_dump($data1['data']);
if try to access key 'data' i get this error
Fatal error: Cannot use object of type CURL as array in * on line 10
So, how i can access the data, direct (the array $data1['data'], and also would you change something from my class to make it better? And for my understanding, why did that return $data; in the first example class not worked. I googled and googled but dont found an answer. Sorry, i just started using OOP before i made a huge list of functions.
UPDATE when i set
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
To false
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
i now can access the data
$data1['data'] , BUT the problem somehow still remains, $data1['data'] gets displayed above at the end i get this:
Fatal error: Cannot use object of type CURL as array in * on line
im just curious, why i cant access if its set true, if somebody can explain that i would be happy.
Thank you for your time.
and
MERRY XMAS TO ALL :-)