1

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 :-)

3 Answers 3

3

What you want is to access the data property of your Curl instance. You do this with the -> operator.

$curlInstance = new Curl("http://www.facebook.com");
$curlInstance->sCURL();
var_dump($curlInstance->data);
Sign up to request clarification or add additional context in comments.

Comments

2

An object cannot be used as an array in that context (it can be in a foreach though).

If data is public, (which it is) you could access it like so:

$data1->data

However, data should be private, and it should be accessed with a getter function:

$data->getData(); //Define getData() to return $this->data

2 Comments

thanks your right. i comment in RageZ answer, about the getter and setter thing, you can see above :)
The getter and setter concept is very simple. You want control over who, how and when your property is accessed. For instance, You want to count the number of times a variable has been accessed, with a getter, you can set a variable and increment it every time the function is called. Without it, you have no control or moderation over the property. Same with setters, you can't control what's messing with your property, where a setter method could do some moderation or validation to see if the desired value matches the program's expectations.
0

In your example:

$data1 = new Curl("http://www.facebook.com");
$data1->sCURL();

var_dump($data1['data']);

$data1 is an object, you cannot use an object as an array(apart if you define a specific interface).

What you can do would be something like

$data1 = new Curl("http://www.facebook.com");
$data1->sCURL();

var_dump($data1->data);

Also it's not generally a good practice to leave public the properties, a better approach is to make the property private and create some methods to access it, it's call getter and setter example:

class CURL {
    private $data;

    ...
    public function getData() {
       return $this->data;
    }

    public function setDate($data) {
       $this->data = $data;
    }
}

Using getter and setter you have a better control over the data, to be noted that in your example the setter won't be needed.

Last point, it's generally a good practice to define al the property, I think you forgot the declaration of the data property in your class.

1 Comment

thank you for your answer, the above works now :-) about that getter and setter, i somehow dont understand it, for what is it good and why it should be in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.