Thursday, January 20, 2011

Php Curl SSL


 check php.info if openssl is installed if there is a version confict it can affect connection 

http://www.php.net/manual/en/function.curl-setopt.php

<?php
require_once 'TibcoConnector.php';
//function sendEncodedImage($image,$claimNumber){
$xmlToSend ="<Pro>.....</Pro>";

try {
    $output = new TibcoConnector();
    $responseData=$output->passXmlToTibco($xmlToSend);
    $parsedOutput=parseResponseData($responseData);
    //    echo $parsedOutput;
    if (strlen($responseData)) {
        //        echo $responseData;
    } else {
        //    echo "Service is Down- Please try again later";
    }
} catch (Exception $e) {
    echo $e->getMessage();
}
//}

function getRandomNumber(){
    srand(time());
    $random = (rand()%100);
    return $random;
}

function getCurrentTime(){
    $time_now=mktime(date('h'),date('i'),date('s'));
    $currentTime= date('h.i.s',$time_now);
    return $currentTime;
}

function parseResponseData($responseData){
    $targetstring = "No Document ID Generated";
    $targetstring1 = "An item already exists for";
    $targetstring2 = "Error";
    $targetstring3 = "SCANNER";
    if(stristr($responseData, $targetstring) === TRUE) {
        return "No Document ID";
    }
    elseif(stristr($responseData, $targetstring1) === TRUE){
        return "This case number is already present";
    }
    elseif(stristr($responseData, $targetstring2) === TRUE){
        return "Error";
    }
    elseif(stristr($responseData, $targetstring3) === TRUE){
        $start=strpos($responseData,$targetstring3);
        $end=strpos($responseData,"Application");
        $inbetweenString = substr($responseData,$start,$start-$end);
        return "Error";
    }
    else{
    //    echo $responseData;
        $modifiedData = str_replace("ns0:", "", $responseData);
        $xml = simplexml_load_string($modifiedData);
        foreach($xml->children()->children()->children() as $child)
        {
            if ($child->getName() ==="CaseNumber"){
                $caseNumber=$child;
            }
        }
        echo $caseNumber;
    }
}
?>


TibcoConnector.php

<?php
require_once 'RemoteConnector.php';
class TibcoConnector{
protected $_error;
protected $_flag=true;
protected $_count=0;
    public function passXmlToTibco($theData){
    //    $url = 'https://......';
        try {
            $output = new Pos_RemoteConnector($url,$theData);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        return $output;
   
}
}


<?php
class Pos_RemoteConnector
{
    protected $_url;
    protected $_remoteFile;
    protected $_error;
    protected $_urlParts;
    protected $_status;
    public    $_flag=true;
    public function __construct($url,$contents)
    {
        $this->_url = $url;
        $this->_contents='xml='.$contents;
        //    $this->checkURL();
        if (function_exists('curl_init')) {
            $this->useCurl();
        } else {
            echo "Site is down-Please be patience";
        }
    }

    protected function checkURL()
    {
        $flags = FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED;
        $urlOK = filter_var($this->_url, FILTER_VALIDATE_URL, $flags);
        $this->_urlParts = parse_url($this->_url);
        if (!$urlOK || $this->_urlParts['scheme'] != 'http') {
            throw new Exception($this->_url . ' is not a valid URL');
        }
        $domainOK = preg_match('/^[^.]+?\.\w{2}/', $this->_urlParts['host']);
        if (!$urlOK) {
            throw new Exception($this->_url . ' is not a valid URL');
        }
    }

    protected function useCurl()
    {

        if ($session = curl_init($this->_url)) {
            curl_setopt($session, CURLOPT_HTTPHEADER,array('Servi:C'));
            curl_setopt($session, CURLOPT_HEADER, 0);
            curl_setopt($session, CURLOPT_POST, 1);
            curl_setopt($session, CURLOPT_POSTFIELDS,$this->_contents);
            curl_setopt($session, CURLOPT_PORT, portnumber integer); //Set the port number
            curl_setopt($session, CURLOPT_TIMEOUT, 1500); // times out after 15s
            curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($session, CURLOPT_SSLVERSION,3);
            curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($session, CURLOPT_VERBOSE, '1');
//            curl_setopt($session, CURLOPT_SSL_VERIFYPEER, true);
//            curl_setopt($session, CURLOPT_SSL_VERIFYHOST, 0);
//            curl_setopt($session, CURLOPT_CAINFO, "/etc/ssl/certs/");
            $this->_remoteFile = curl_exec($session);
            $this->_status = curl_getinfo($session, CURLINFO_HTTP_CODE);
            if(curl_errno($session))
            {
                echo 'error:' . curl_error($session);
            }
               
            curl_close($session);
        } else {
            $this->_error = 'Cannot establish cURL session';
        }
    }
    public function __toString()
    {
            $returnString=$this->_remoteFile;
     if(isset($returnString)&&!empty($returnString)){
         //echo $this->_remoteFile;
         return $this->_remoteFile;
     }
     else{
     //    var_dump($this->_remoteFile);
     //    echo $this->_remoteFile;
         //return "Tibco Call failed-Try one more time";
     }
    }

    public function getErrorMessage()
    {
        if (is_null($this->_error)) {
            $this->setErrorMessage();
        }
        return $this->_error;
    }

    protected function removeHeaders()
    {
        $parts = preg_split('#\r\n\r\n|\n\n#', $this->_remoteFile);
        if (is_array($parts)) {
            $headers = array_shift($parts);
            $file = implode("\n\n", $parts);
            if (preg_match('#HTTP/1\.\d\s+(\d{3})#', $headers, $m)) {
                $this->_status = $m[1];
            }
            if (preg_match('#Content-Type:([^\r\n]+)#i', $headers, $m)) {
                if (stripos($m[1], 'xml') !== false || stripos($m[1], 'html') !== false) {
                    if (preg_match('/<.+>/s', $file, $m)) {
                        $this->_remoteFile = $m[0];
                    } else {
                        $this->_remoteFile = trim($file);
                    }
                } else {
                    $this->_remoteFile = trim($file);
                }
            }
        }
    }

    protected function setErrorMessage()
    {
        if ($this->_status == 200 && $this->_remoteFile) {
            $this->_error = '';
        } else {
            switch ($this->_status) {
                case 200:
                case 204:
                    $this->_error = 'Connection OK, but file is empty.';
                    break;
                case 301:
                case 302:
                case 303:
                case 307:
                case 410:
                    $this->_error = 'File has been moved or does not exist.';
                    break;
                case 305:
                    $this->_error = 'File must be accessed through a proxy.';
                    break;
                case 400:
                    $this->_error = 'Malformed request.';
                    break;
                case 401:
                case 403:
                    $this->_error = 'You are not authorized to access this page.';
                    break;
                case 404:
                    $this->_error = 'File not found.';
                    break;
                case 407:
                    $this->_error = 'Proxy requires authentication.';
                    break;
                case 408:
                    $this->_error = 'Request timed out.';
                    break;
                case 500:
                    $this->_error = 'The remote server encountered an internal error.';
                    break;
                case 503:
                    $this->_error = 'The server cannot handle the request at the moment.';
                    break;
                default:
                    $this->_error = 'Undefined error. Check URL and domain name.';
                    break;
            }
        }
    }
}

No comments:

Post a Comment