writing complex CURL class

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
vegeta
Forum Newbie
Posts: 1
Joined: Wed May 03, 2006 10:26 am

writing complex CURL class

Post by vegeta »

Hello,

I try to communicate with a server,
i think it is bettet to write a class for this task.
If I read the class variable, there are no values.
but in the debuge mode i can see the correct values.

thanks for help

here the code:

Code: Select all

<?php 
class CurlReader {
    var $body;
    var $headers;
    var $c;
    function CurlReader() {
        $iheaders[]= "Content-Type: text/xml";
        $iheaders[]= "Pragma: no-cache";
        $this->c = curl_init();
        curl_setopt($this->c, CURLOPT_HEADER, 1);
        curl_setopt($this->c, CURLOPT_SSL_VERIFYPEER, "false");
        curl_setopt($this->c, CURLOPT_HTTPHEADER, $iheaders);
        curl_setopt($this->c, CURLOPT_POST, 1);
        curl_setopt($this->c, CURLOPT_POSTFIELDS, 'etwas');
        curl_setopt($this->c, CURLOPT_HEADERFUNCTION, array($this,'parseHeader'));
        curl_setopt($this->c, CURLOPT_WRITEFUNCTION, array($this, 'parseBody'));
    }
   function parseHeader($curl, $data) 
   {
       list($header, $value) = explode(': ', $data, 2);
       $this->headers[$header] = value;
    }
    }
   function parseBody($curl, $data) 
   {
        $this->body .= $data;
    }
     function get($url) {
        $this->headers = array();
        $this->body = '';
        curl_setopt($this->c, CURLOPT_URL, $url);               
        return curl_exec($this->c);
     }
    function close()
    {
         curl_close($this->c);
    }
     function getbody()
    {
        return $this->body;
     }
}
    $c->get('https://www.server.de);
     print $c->getbody();
     $erg= $c->getbody();

$c->close();

?>
Post Reply