cURL Example : API

 

cURL PHP Class Example

the following example is for curl exchanges with the cloud:

<?php


if (!function_exists("getIPData")) {
    function 
getIPData($ip=false){
        
$ret = array();
        if (
is_object($GLOBALS['xoopsUser'])) {
            
$ret['uid'] = $GLOBALS['xoopsUser']->getVar('uid');
            
$ret['uname'] = $GLOBALS['xoopsUser']->getVar('uname');
            
$ret['email'] = $GLOBALS['xoopsUser']->getVar('email');
        } else {
            
$ret['uid'] = 0;
            
$ret['uname'] = $_REQUEST['uname'];
            
$ret['email'] = $_REQUEST['email'];
        }
        if (!
$ip) {
            if (
$_SERVER["HTTP_X_FORWARDED_FOR"] != ""){ 
                
$ip = (string)$_SERVER["HTTP_X_FORWARDED_FOR"]; 
                
$ret['is_proxied'] = true;
                
$proxy_ip $_SERVER["REMOTE_ADDR"]; 
                
$ret['network-addy'] = @gethostbyaddr($ip); 
                
$ret['long'] = @ip2long($ip);
                if (
is_ipv6($ip)) {
                    
$ret['ip6'] = $ip;
                    
$ret['proxy-ip6'] = $proxy_ip;
                } else {
                    
$ret['ip4'] = $ip;
                    
$ret['proxy-ip4'] = $proxy_ip;
                }
            }else{ 
                
$ret['is_proxied'] = false;
                
$ip = (string)$_SERVER["REMOTE_ADDR"]; 
                
$ret['network-addy'] = @gethostbyaddr($ip); 
                
$ret['long'] = @ip2long($ip);
                if (
is_ipv6($ip)) {
                    
$ret['ip6'] = $ip;
                } else {
                    
$ret['ip4'] = $ip;
                }
            } 
        } else {
            
$ret['is_proxied'] = false;
            
$ret['network-addy'] = @gethostbyaddr($ip); 
            
$ret['long'] = @ip2long($ip);
            if (
is_ipv6($ip)) {
                
$ret['ip6'] = $ip;
            } else {
                
$ret['ip4'] = $ip;
            }
        }
        
$ret['made'] = time();                
        return 
$ret;
    }
}

if (!
function_exists("is_ipv6")) {
    function 
is_ipv6($ip ""
    { 
        if (
$ip == ""
            return 
false;
            
        if (
substr_count($ip,":") > 0){ 
            return 
true
        } else { 
            return 
false
        } 
    } 
}

if (!
function_exists("xortify_obj2array")) {
    function 
xortify_obj2array($objects) {
        
$ret = array();
        foreach(
$objects as $key => $value) {
            if (
is_a($value'stdClass')) {
                
$ret[$key] = xortify_obj2array((array)$value);
            } elseif (
is_array($value)) {
                
$ret[$key] = xortify_obj2array($value);
            } else {
                
$ret[$key] = $value;
            }
        }
        return 
$ret;
    }
}

foreach (
get_loaded_extensions() as $ext){
    if (
$ext=="curl")
        
$nativecurl=true;
}

if (
$nativecurl==true) {
    
define('XOOPS_CURL_LIB''PHPCURL');
    
define('XORTIFY_USER_AGENT''Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) XOOPS/20100101 XoopsAuth/1.xx (php)');
}

define('XORTIFY_CURL_API''http://xortify.chronolabs.coop/curl/);

if (!class_exists('
Services_JSON'))
    include_once('
/pear/JSON.php');

class CURLXortifyExchange {

    var $curl_client;
    var $curl_xoops_username = '
user';
    var $curl_xoops_password = md5('
pass');
    var $refresh = 600;
    var $json = '';
    
    function __construct()
    {
        $this->CURLXortifyExchange ();
    }
    
    function CURLXortifyExchange () {
        $this->json = new Services_JSON();

        if (!$ch = curl_init(XORTIFY_CURL_API)) {
            trigger_error('
Could not intialise CURL file'.$url);
            return false;
        }
        $cookies = XOOPS_VAR_PATH.'
/cache/xoops_cache/authcurl_'.md5(XORTIFY_CURL_API).'.cookie'; 

        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_USERAGENT, XORTIFY_USER_AGENT); 
        $this->curl_client =& $ch;            
    }
    

    function sendBan($comment, $category_id = 2, $ip=false) {
        @$this->CURLXortifyExchange();        
        $ipData = getIPData($ip);
        switch (XOOPS_CURL_LIB){
        case "PHPCURL":
            curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, '
ban='.$this->json->encode(array(      "username"    =>     $this->curl_xoops_username, 
                            "password"    =>     $this->curl_xoops_password, 
                            "bans"         =>     array(    0     =>     array_merge(
                                                                        $ipData, 
                                                                        array('
category_id' => $category_id)
                                                                        )
                                            ),
                            "comments"     =>     array(    0    =>    array(    '
uname'        =>        $this->curl_xoops_username, 
                                                                    "comment"     =>         $comment
                                                            )
                                             ) )));
            $data = curl_exec($this->curl_client);
            curl_close($this->curl_client);
            $result = xortify_obj2array($this->json->decode($data));        
            break;
        }
        return $result;    
    }

    function checkBans($ipdata) {
        @$this->CURLXortifyExchange();        
        switch (XOOPS_CURL_LIB){
        case "PHPCURL":
            curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, '
banned='.$this->json->encode(array(      "username"    =>     $this->curl_xoops_username, 
                            "password"    =>     $this->curl_xoops_password, 
                            "ipdata"     =>     $ipdata
                        )));
            $data = curl_exec($this->curl_client);
            curl_close($this->curl_client);
            $result = xortify_obj2array($this->json->decode($data));        
            break;
        }
        return $result;    
    }

    function checkUnbans($ipdata) {
        @$this->CURLXortifyExchange();        
        switch (XOOPS_CURL_LIB){
        case "PHPCURL":
            curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, '
unbanned='.$this->json->encode(array(      "username"    =>     $this->curl_xoops_username, 
                            "password"    =>     $this->curl_xoops_password, 
                            "ipdata"     =>     $ipdata
                        )));
            $data = curl_exec($this->curl_client);
            curl_close($this->curl_client);
            $result = xortify_obj2array($this->json->decode($data));        
            break;
        }
        return $result;    
    }
    
    function checkSFSBans($ipdata) {
        @$this->CURLXortifyExchange();        
        switch (XOOPS_CURL_LIB){
        case "PHPCURL":
            curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, '
checksfsbans='.$this->json->encode(array(      "username"    =>     $this->curl_xoops_username, 
                            "password"    =>     $this->curl_xoops_password, 
                            "ipdata"     =>     array_merge($ipdata, array('
category_id' => $category_id))
                        )));
            $data = curl_exec($this->curl_client);
            curl_close($this->curl_client);
            $result = xortify_obj2array($this->json->decode($data));        
            break;
        }
        return $result;    
    }

    function checkPHPBans($ipdata) {
        @$this->CURLXortifyExchange();        
        switch (XOOPS_CURL_LIB){
        case "PHPCURL":
            curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, '
checkphpbans='.$this->json->encode(array(      "username"    =>     $this->curl_xoops_username, 
                            "password"    =>     $this->curl_xoops_password, 
                            "ipdata"     =>     $ipdata
                        )));
            $data = curl_exec($this->curl_client);
            curl_close($this->curl_client);
            $result = xortify_obj2array($this->json->decode($data));        
            break;
        }
        return $result;    
    }
    
    function retrieveBans() {
        @$this->CURLXortifyExchange();        
        switch (XOOPS_CURL_LIB){
        case "PHPCURL":
            curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, '
bans='.$this->json->encode(array("username"=> $this->curl_xoops_username, "password"=> $this->curl_xoops_password,  "records"=> $this->refresh)     ) );
            $data = curl_exec($this->curl_client);
            curl_close($this->curl_client);                
            $result = xortify_obj2array($this->json->decode($data));        
        }
        return $result;
    }

    function retrieveUnbans() {
        @$this->CURLXortifyExchange();        
        switch (XOOPS_CURL_LIB){
        case "PHPCURL":
            curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, '
unbans='.$this->json->encode(array("username"=> $this->curl_xoops_username, "password"=> $this->curl_xoops_password,  "records"=> $this->refresh)     ) );
            $data = curl_exec($this->curl_client);
            curl_close($this->curl_client);                
            $result = xortify_obj2array($this->json->decode($data));        
        }
        return $result;
    }

    function sendSpider($spider) {
        @$this->CURLXortifyExchange();
        switch (XOOPS_CURL_LIB){
        case "PHPCURL":
            curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, '
spider='.$this->json->encode(array(      "username"    =>     $this->curl_xoops_username, "password"    =>     $this->curl_xoops_password, "spider"    =>     $spider    )));
            $data = curl_exec($this->curl_client);
            xoops_error($data, '
cURL Result');
            exit(0);
            curl_close($this->curl_client);
            $result = xortify_obj2array($this->json->decode($data));        
            break;
        }
            
        return $result;    
    }
    
    function sendStatistic($statistic) {
        @$this->CURLXortifyExchange();
        switch (XOOPS_CURL_LIB){
        case "PHPCURL":
            curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, '
spiderstat='.$this->json->encode(array(      "username"    =>     $this->curl_xoops_username, "password"    =>     $this->curl_xoops_password, "statistic"    =>     $statistic    )));
            $data = curl_exec($this->curl_client);
            curl_close($this->curl_client);
            $result = xortify_obj2array($this->json->decode($data));        
            break;
        }
            
        return $result;    
    }
    
    function getSpiders() {
        @$this->CURLXortifyExchange();        
        switch (XOOPS_CURL_LIB){
        case "PHPCURL":
            curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, '
spiders='.$this->json->encode(array( "username" => $this->curl_xoops_username, "password"    =>     $this->curl_xoops_password )));
            $data = curl_exec($this->curl_client);
            curl_close($this->curl_client);
            $result = xortify_obj2array($this->json->decode($data));        
            break;
        }
            
        return $result['
robots'];
    }
    
    function getSEOLinks() {
        @$this->CURLXortifyExchange();        
        switch (XOOPS_CURL_LIB){
        case "PHPCURL":
            curl_setopt($this->curl_client, CURLOPT_POSTFIELDS, '
seolinks='.$this->json->encode(array( "username" => $this->curl_xoops_username, "password"    =>     $this->curl_xoops_password )));
            $data = curl_exec($this->curl_client);
            curl_close($this->curl_client);    
            $result = xortify_obj2array($this->json->decode($data));        
        
            break;
        }            
        return $result['
seolinks];        
    }    
}
Written by: System Overlord 2011/4/21