[PHP] cURL class

Gestart doorRik14 reacties42 weergaven

15 berichten · pagina 1 van 1

Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Omdat iemand er om vroeg op msn (weet niet meer wie) bij deze. Graag copyright e.d. laten staan. Exceptions moet je even aanpassen naar je eigen exceptionhandler, maar deze werkte met mijn eigen exceptionhandler voor MSL/GLS.

Donaties altijd welkom ^^

[COLOR= #0000BB]<?php[/COLOR]

    [COLOR= #FF8000]/**
     * Curl Class
     * Handles curl requests made to other sites.
     *
     * @author      Rik S. & Luuk vd O.
     * @copyright   Copyright (c) Rik S. & Luuk vd O.  All rights reserved.
     */[/COLOR]
 
 [URL="http://www.php.net/class"]class[/URL] curl
 [COLOR= #007700]{[/COLOR]
    
    [COLOR= #FF8000]/**
    * Contains all values which have to be posted by cURL
    * 
    * @var array
    */[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$arrPostFields[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();[/COLOR]
    
    [COLOR= #FF8000]/**
    * Vars containing information about our fake browser
    * - User agent
    *  
    * @var string
    */[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$strUserAgent[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]Opera/9.80 (Windows NT 5.1; U; nl) Presto/2.5.22 Version/10.51[/COLOR][COLOR= #007700];[/COLOR]
    
    [COLOR= #FF8000]/**
    * Vars containing information about our fake browser
    * - Refferer
    *  
    * @var string
    */[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$strReffer[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]http://www.google.nl[/COLOR][COLOR= #007700];[/COLOR]
    
    [COLOR= #FF8000]/**
    * Location of file to store cookie information
    *  
    * @var string
    */[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$strCookieFile[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #DD0000]cookie.txt[/COLOR][COLOR= #007700];[/COLOR]
    
    [COLOR= #FF8000]/**
    * Holds content which is saved by a cURL request
    * 
    * @var string
    */[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #0000BB]$strCurlContent[/COLOR] [COLOR= #007700]=[/COLOR] NULL[COLOR= #007700];[/COLOR]
    
    [COLOR= #FF8000]/**
    * Holds url which has to be posted
    * 
    * @var string
    */[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #0000BB]$strUrl[/COLOR] [COLOR= #007700]=[/COLOR] NULL[COLOR= #007700];[/COLOR]
    
    [COLOR= #FF8000]/**
    * Holds the identifier of current cURL request
    * 
    * @var resource
    */[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$resCurlHandler[/COLOR] [COLOR= #007700]=[/COLOR] NULL[COLOR= #007700];[/COLOR] 
     
    [COLOR= #FF8000]/**
    * Set fake browser information if needed
    * 
    * @param string $strUserAgent
    * @param string $strReffer
    * @param string $strCookieFile
    */[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] __construct[COLOR= #007700]([/COLOR][COLOR= #0000BB]$strUserAgent[/COLOR] [COLOR= #007700]=[/COLOR] NULL[COLOR= #007700],[/COLOR] [COLOR= #0000BB]$strReffer[/COLOR] [COLOR= #007700]=[/COLOR] NULL[COLOR= #007700],[/COLOR] [COLOR= #0000BB]$strCookieFile[/COLOR] [COLOR= #007700]=[/COLOR] NULL[COLOR= #007700])
    {[/COLOR]
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][URL="http://www.php.net/is_null"]is_null[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$strUserAgent[/COLOR][COLOR= #007700]) ===[/COLOR] TRUE[COLOR= #007700])
        {[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]strUserAgent [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$strUserAgent[/COLOR][COLOR= #007700];
        }[/COLOR]
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][URL="http://www.php.net/is_null"]is_null[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$strReffer[/COLOR][COLOR= #007700]) ===[/COLOR] TRUE[COLOR= #007700])
        {[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]strReffer [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$strReffer[/COLOR][COLOR= #007700];
        }[/COLOR]
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][URL="http://www.php.net/is_null"]is_null[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$strCookieFile[/COLOR][COLOR= #007700]) ===[/COLOR] TRUE[COLOR= #007700])
        {[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]strCookieFile [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$strCookieFile[/COLOR][COLOR= #007700];
        }
    }[/COLOR]
    
    [COLOR= #FF8000]/**
    * Set vars to be posted (This will reset previous post vars)
    * 
    * @param array $arrPostFields
    */[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] setPost[COLOR= #007700]([/COLOR][COLOR= #0000BB]$arrPostFields[/COLOR] [COLOR= #007700]=[/COLOR] NULL[COLOR= #007700])
    {[/COLOR]
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][URL="http://www.php.net/is_null"]is_null[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$arrPostFields[/COLOR][COLOR= #007700]))
        {[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]arrPostFields [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$arrPostFields[/COLOR][COLOR= #007700];
        }
    }[/COLOR]
    
    [COLOR= #FF8000]/**
    * Set url to go to (This will reset previous urls)
    * 
    * @param array $url
    */[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] setUrl[COLOR= #007700]([/COLOR][COLOR= #0000BB]$strUrl[/COLOR] [COLOR= #007700]=[/COLOR] NULL[COLOR= #007700])
    {[/COLOR]
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][URL="http://www.php.net/is_null"]is_null[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$strUrl[/COLOR][COLOR= #007700]))
        {[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]strUrl [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$strUrl[/COLOR][COLOR= #007700];
        }
    }[/COLOR]
    
    [COLOR= #FF8000]/**
    * Execute cURL request and reset cURL if needed
    * 
    * @param string $strUrl
    * @param boolean $boolReset
    */[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] execute[COLOR= #007700]([/COLOR][COLOR= #0000BB]$boolReset[/COLOR] [COLOR= #007700]=[/COLOR] TRUE[COLOR= #007700])
    {[/COLOR]
        [COLOR= #FF8000]/**
        * Throw an error if url is not set.
        */[/COLOR]
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/is_null"]is_null[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]strUrl[COLOR= #007700]))
        {[/COLOR]
           [COLOR= #FF8000]// throw new Exception(Curl request is made to empty url, 30);[/COLOR]
        [COLOR= #007700]}[/COLOR]    
        
        [COLOR= #FF8000]/**
        * Initializes curl and save resource identifier.
        */[/COLOR]
        [COLOR= #0000BB]$resCurlHandler[/COLOR]     [COLOR= #007700]=[/COLOR] curl_init[COLOR= #007700]();[/COLOR]
        [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]resCurlHandler [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$resCurlHandler[/COLOR][COLOR= #007700];[/COLOR]
        
        [COLOR= #FF8000]/**
        * Build the HTTP-header information.
        */[/COLOR]
        [COLOR= #0000BB]$arrHttpHeader[/COLOR]     [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();[/COLOR]
        [COLOR= #0000BB]$arrHttpHeader[/COLOR][COLOR= #007700][]   =[/COLOR] [COLOR= #DD0000]Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8[/COLOR][COLOR= #007700];[/COLOR]
        [COLOR= #0000BB]$arrHttpHeader[/COLOR][COLOR= #007700][]   =[/COLOR] [COLOR= #DD0000]Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1[/COLOR][COLOR= #007700];[/COLOR] 
        [COLOR= #0000BB]$arrHttpHeader[/COLOR][COLOR= #007700][]   =[/COLOR] [COLOR= #DD0000]Accept-language: nl-NL,nl;q=0.9,en;q=0.8[/COLOR][COLOR= #007700];[/COLOR]
        [COLOR= #0000BB]$arrHttpHeader[/COLOR][COLOR= #007700][]   =[/COLOR] [COLOR= #DD0000]Accept-Charset: iso-8859-1, utf-8, utf-16, *;q=0.1[/COLOR][COLOR= #007700];[/COLOR]

        [COLOR= #FF8000]/**
        * Set curl options.
        */[/COLOR]
        curl_setopt [COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]resCurlHandler[COLOR= #007700],[/COLOR] CURLOPT_URL[COLOR= #007700],[/COLOR] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]strUrl[COLOR= #007700]);[/COLOR]
        curl_setopt [COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]resCurlHandler[COLOR= #007700],[/COLOR] CURLOPT_REFERER[COLOR= #007700],[/COLOR] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]strReffer[COLOR= #007700]);[/COLOR]
        curl_setopt [COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]resCurlHandler[COLOR= #007700],[/COLOR] CURLOPT_USERAGENT[COLOR= #007700],[/COLOR] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]strUserAgent[COLOR= #007700]);[/COLOR]
        curl_setopt [COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]resCurlHandler[COLOR= #007700],[/COLOR] CURLOPT_RETURNTRANSFER[COLOR= #007700],[/COLOR] [COLOR= #0000BB]1[/COLOR][COLOR= #007700]);[/COLOR] 
        curl_setopt [COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]resCurlHandler[COLOR= #007700],[/COLOR] CURLOPT_VERBOSE[COLOR= #007700],[/COLOR] [COLOR= #0000BB]1[/COLOR][COLOR= #007700]);[/COLOR]
        curl_setopt [COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]resCurlHandler[COLOR= #007700],[/COLOR] CURLOPT_HTTPHEADER[COLOR= #007700],[/COLOR] [COLOR= #0000BB]$arrHttpHeader[/COLOR][COLOR= #007700]);[/COLOR]
        curl_setopt [COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]resCurlHandler[COLOR= #007700],[/COLOR] CURLOPT_COOKIEFILE[COLOR= #007700],[/COLOR] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]strCookieFile[COLOR= #007700]);[/COLOR]
        curl_setopt [COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]resCurlHandler[COLOR= #007700],[/COLOR] CURLOPT_COOKIEJAR[COLOR= #007700],[/COLOR] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]strCookieFile[COLOR= #007700]);[/COLOR]
        
        [COLOR= #FF8000]/**
        * If post values have been added, make a post request.
        */[/COLOR]
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][URL="http://www.php.net/empty"]empty[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]arrPostFields[COLOR= #007700]))
        {[/COLOR]
            curl_setopt[COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]resCurlHandler[COLOR= #007700],[/COLOR] CURLOPT_POST[COLOR= #007700],[/COLOR] [COLOR= #0000BB]1[/COLOR][COLOR= #007700]);[/COLOR]
            curl_setopt[COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]resCurlHandler[COLOR= #007700],[/COLOR] CURLOPT_POSTFIELDS[COLOR= #007700],[/COLOR] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]arrPostFields[COLOR= #007700]);[/COLOR]
            
            [COLOR= #FF8000]/**
            * If reset is true, reset postvars, else hold the vars for further use.
            */[/COLOR]
            [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$boolReset[/COLOR] [COLOR= #007700]===[/COLOR] TRUE[COLOR= #007700])
            {[/COLOR]
                [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]arrPostFields [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();
            }
        }[/COLOR]
        
        [COLOR= #FF8000]/**
        * Execute the request and save page source.
        */[/COLOR]
        [COLOR= #0000BB]$resTemp[/COLOR] [COLOR= #007700]=[/COLOR] curl_exec[COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]resCurlHandler[COLOR= #007700]);[/COLOR]
        [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]strCurlContent [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$resTemp[/COLOR][COLOR= #007700];[/COLOR]
        
        [COLOR= #FF8000]/**
        * Set reffer to be the previous url so login systems think we are a normal user.
        */[/COLOR]
        [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]strReffer [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]strUrl[COLOR= #007700];[/COLOR]
        
        [COLOR= #FF8000]/**
        * If an error has occured, throw the error.
        */[/COLOR]
        [URL="http://www.php.net/if"]if[/URL] [COLOR= #007700]([/COLOR]curl_errno[COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]resCurlHandler[COLOR= #007700]))
        {[/COLOR]
            [COLOR= #FF8000]//throw new Exception(Curl request could not be made:  . curl_error($resCurlHandler), 30);[/COLOR]
        [COLOR= #007700]}
    }[/COLOR]
    
    [COLOR= #FF8000]/**
    * Close current cURL resource
    * 
    * @return TRUE (on succes)
    * @return FALSE (on failure)
    */[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] close[COLOR= #007700]()
    {[/COLOR]
        [COLOR= #FF8000]/**
        * If a recource identifier can be found close it and clear vars, else throw an error.
        */[/COLOR]
        [URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][URL="http://www.php.net/is_null"]is_null[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]resCurlHandler[COLOR= #007700]))
        {[/COLOR]
            curl_close[COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]resCurlHandler[COLOR= #007700]);[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]resCurlHandler [COLOR= #007700]=[/COLOR] NULL[COLOR= #007700];[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]arrPostFields [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();[/COLOR]
            [URL="http://www.php.net/return"]return[/URL] TRUE[COLOR= #007700];
        }[/COLOR]
        [URL="http://www.php.net/else"]else[/URL]
        [COLOR= #007700]{[/COLOR]
            [COLOR= #FF8000]// throw new Exception(No instance of curl could be found , 30);[/COLOR]
            [URL="http://www.php.net/return"]return[/URL] FALSE[COLOR= #007700];        
        }
    }[/COLOR]
    
    [COLOR= #FF8000]/**
    * Returns cURLs content.
    * 
    * @return string
    */[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] PrintContent[COLOR= #007700]()
    {[/COLOR]
        [URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]strCurlContent[COLOR= #007700];
    }
 }[/COLOR]

?>


voorbeeld:

$cHTTP = new curl();
        $cHTTP->setUrl(http://www.mywerewolf.nl);
        $cHTTP->execute();
        echo $cHTTP->printContent();
        $cHTTP->close();
0
Berichten
2.837
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Sorry hoor maar wat is hier en praktische nut van yummie..
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Content van een andere site halen / Inloggen op een andere site via een PHP script.
0
Berichten
1.168
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Tof script!
thanks!
0
Berichten
970
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Oke, maar hoe kun je inloggen?
0
Berichten
4.912
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
k snap het ook niet zou je kunnen uitleggen hoe het werktbiggrin
0
Berichten
20.364
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Dat is simpel, gewoon url invullen. emo Gewoon lezen wat er staat.
0
Berichten
4.912
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
hoe haal ik het inloggen dan op?
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
$cHTTP = new curl();
        $cHTTP->setUrl(MijnSite.nl/inloggen);
        $cHTTP->setPost(array("Naam" => "Rik", "Wachtwoord" => "Mijn wachtwoord"));
        $cHTTP->execute();
        $cHTTP->setUrl(MijnSite.nl/beveiligdepagina);
        echo $cHTTP->printContent();
        $cHTTP->close();
0
Berichten
4.912
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
$cHTTP = new curl();
        $cHTTP->setUrl(MijnSite.nl/inloggen);
        $cHTTP->setPost(array("Naam" => "Rik", "Wachtwoord" => "Mijn wachtwoord"));
        $cHTTP->execute();
        $cHTTP->setUrl(MijnSite.nl/beveiligdepagina);
        echo $cHTTP->printContent();
        $cHTTP->close();


waar moet ik dat neerzettenbiggrin
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Meen je dat nou serieus...
0
Berichten
4.912
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Rik schreef:
Meen je dat nou serieus...
je denkt dat ik me postjes hier +1 zit te zetten
nee ik snap het niet maar ik wil het snappenbloos
0
Berichten
20.364
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Als je dit nog niet snapt, dan moet je nog niet met dit beginnen. Wel cool, kan dit als proxy gebruiken.
0
Berichten
5.795
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Shahiem schreef:
Als je dit nog niet snapt, dan moet je nog niet met dit beginnen. Wel cool, kan dit als proxy gebruiken.
als je het als echte web proxy wil gaan gebruiken moet je toch alle urls rewirten hea.
0
Berichten
3.540
Reputatie
0
Punten
0,01
Lid sinds
Laatst gezien
Niet noodzakelijk toch?
Een proxy is gebouwd om naar een site met een ander IP te surfen, en in dit geval doe je dat toch biggrin
0

Jouw reactie

Deel je idee, stel een vraag of help een ander verder.

Editor laden…

Praat mee met de One2xs-community.

Inloggen om te reageren