Datei downloaden mit cUrl

Ich stelle hier einfach mal eine Klasse bereit mit der einfache Requests an z.b. einen Webserver stellen kann.

Es lässt sich der anfragende Useragent einstellen (um z.b. einen Webbrowser zu simulieren)
und falls man ein Script für die Console entwickelt lassen sich auch „lognachrichten“ ausgeben.

Hier aber erstmal der Code:

<?php 
 
class basespider
{
    protected $useragent = null;
    protected $cookie = null;
    protected $followlocation = null;
    protected $post = null;
    protected $get = null;
    protected $consolenOutput = false;
 
    /**
     * Constructor mit Grundeinstellungen
     *
     * @param string $useragent        //Useragent unter dem angefragt wird
     * @param bool $consolenOutput    //Soll eine art Log ausgegeben werden?
     */
    public function __construct(
        $useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1) Gecko/20061010 Firefox/2.0',
        $consolenOutput = false
    )
    {
        $this->useragent = $useragent;
        $this->cookie = "data/cookie.txt";
        $this->followlocation = 1;
        $this->timeout = 30;
        $this->consolenOutput = $consolenOutput;
    }
 
    /**
     * Setzt post-parameter
     *
     * @param array $daten
     */
    public function setPost($daten)
    {
        if($daten != null)
        {
            $tmp = "";
            foreach($daten as $key => $value)
            {
                $tmp .= $key."="./*urlencode*/($value)."&";
            }
 
            $this->post = rtrim($tmp, '&');
        }
 
    }
 
    /**
     * Setzt get-parameter
     *
     * @param array $daten
     */
    public function setGet($daten)
    {
        if($daten != null)
        {
            $tmp = "?";
            foreach($daten as $key => $value)
            {
                $tmp .= $key."=".$value."&";
            }
 
            $this->get = rtrim($tmp, '&');
        }
    }
 
    /**
     * setzt einen query zur angegeben url ab und löscht nach dem senden post und get parameter
     *
     * @param string $url
     * @return mixed
     */
    public function Query($url)
    {
        if(isset($this->get) && $this->get !== null)
            $url .= $this->get;
 
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
 
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->followlocation);
 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, $this->useragent);
 
        if($this->cookie !== null)
        {
            curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookie);
            curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookie);
        }
 
        if(isset($this->post) && $this->post !== null)
        {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$this->post);
 
        }
 
        if($this->consolenOutput)
        {
            echo date("d-m-Y H:i:s").": $url".(isset($this->post) ? ' ('.$this->post.')' :'')."\n";
        }
 
        $return = curl_exec($ch);
 
        if(curl_errno($ch) && $this->consolenOutput)
        {
            echo curl_error($ch);
        }
        curl_close($ch);
 
        unset($this->get);
        unset($this->post);
 
        return $return;
    }
}
 
$spider = new basespider();
 
$res = $spider->Query('http://www.google.de/');
 
?>
 
<html>
    <head>
        <title>Spidertest</title>
    </head>
 
    <body>


<div style="border: 1px solid black; width: 700px; height: 300px; overflow:auto;"><?=$res?></div>


    </body>
</html>

In diesem Beispiel laden wir die startseite von Google.

Bevor man die Querymethode aufruft lassen sich aber auch GET und POST Parameter mit einbinden.
Dafür gibt es die Methoden setGet und setPost die jeweils als array übergeben werden.

Das ergebnis $res kann man nun wahlweise ausgeben oder in eine Datei schreiben, z.b. mit der Funktion file_put_contents

Das wars dann auch schon.

Viel spass damit!