- /**
- * CURL encapsulation class
- * By bbs.it-home.org
- */
- Class Curl_Class
- {
- Function Curl_Class ()
- {
- Return true;
- }
- Function execute ($ method, $ url, $ fields = '', $ userAgent ='', $ httpHeaders = '', $ username ='', $ password = '')
- {
- $ Ch = Curl_Class: create ();
- If (false ===$ ch)
- {
- Return false;
- }
- If (is_string ($ url) & strlen ($ url ))
- {
- $ Ret = curl_setopt ($ ch, CURLOPT_URL, $ url );
- }
- Else
- {
- Return false;
- }
- // Whether the header information is displayed
- Curl_setopt ($ ch, CURLOPT_HEADER, false );
- //
- Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
- If ($ username! = '')
- {
- Curl_setopt ($ ch, CURLOPT_USERPWD, $ username. ':'. $ password );
- }
- $ Method = strtolower ($ method );
- If ('post' = $ method)
- {
- Curl_setopt ($ ch, CURLOPT_POST, true );
- If (is_array ($ fields ))
- {
- $ Sets = array ();
- Foreach ($ fields AS $ key => $ val)
- {
- $ Sets [] = $ key. '='. urlencode ($ val );
- }
- $ Fields = implode ('&', $ sets );
- }
- Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields );
- }
- Else if ('put' = $ method)
- {
- Curl_setopt ($ ch, CURLOPT_PUT, true );
- }
- // Curl_setopt ($ ch, CURLOPT_PROGRESS, true );
- // Curl_setopt ($ ch, CURLOPT_VERBOSE, true );
- // Curl_setopt ($ ch, CURLOPT_MUTE, false );
- Curl_setopt ($ ch, CURLOPT_TIMEOUT, 3); // sets the curl timeout time in seconds. for example, the system automatically stops running after three seconds of posting information.
- If (strlen ($ userAgent ))
- {
- Curl_setopt ($ ch, CURLOPT_USERAGENT, $ userAgent );
- }
- If (is_array ($ httpHeaders ))
- // Bbs.it-home.org
- Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ httpHeaders );
- }
- $ Ret = curl_exec ($ ch );
- If (curl_errno ($ ch ))
- {
- Curl_close ($ ch );
- Return array (curl_error ($ ch), curl_errno ($ ch ));
- }
- Else
- {
- Curl_close ($ ch );
- If (! Is_string ($ ret) |! Strlen ($ ret ))
- {
- Return false;
- }
- Return $ ret;
- }
- }
- Function post ($ url, $ fields, $ userAgent = '', $ httpHeaders ='', $ username = '', $ password = '')
- {
- $ Ret = Curl_Class: execute ('post', $ url, $ fields, $ userAgent, $ httpHeaders, $ username, $ password );
- If (false ===$ ret)
- {
- Return false;
- }
- If (is_array ($ ret ))
- {
- Return false;
- }
- Return $ ret;
- }
- Function get ($ url, $ userAgent = '', $ httpHeaders ='', $ username = '', $ password = '')
- {
- $ Ret = Curl_Class: execute ('GET', $ url, '', $ userAgent, $ httpHeaders, $ username, $ password );
- If (false ===$ ret)
- {
- Return false;
- }
- If (is_array ($ ret ))
- {
- Return false;
- }
- Return $ ret;
- }
- Function create ()
- {
- $ Ch = null;
- If (! Function_exists ('curl _ init '))
- {
- Return false;
- }
- $ Ch = curl_init ();
- If (! Is_resource ($ ch ))
- {
- Return false;
- }
- Return $ ch;
- }
- }
|