src/Core.php
/**
     * Sends an api request through the use of CURL
     * @param string url - the url where the api is located.
     * @param array parameters - name value pairs for sending to the api server
     * @param string $requestType - the request type. One of GET, POST, PUT, PATCH or DELETE
     * @param array $headers - name/value pairs for headers. Useful for authentication etc.
     * @return stdObject - json response object from the api server
     * @throws \Exception
     */
Sends an api request through the use of CURL
@param string url - the url where the api is located.
@param array parameters - name value pairs for sending to the api server
@param string $requestType - the request type.
Sends an api request through the use of CURL @param string url - the url where the api is located . @param array parameters - name value pairs for sending to the api server @param string $requestType - the request type .
public static function sendApiRequest(
        $url, 
        array $parameters, 
        $requestType="POST", 
        $headers=array()
    )
    {
        $allowedRequestTypes = array("GET", "POST", "PUT", "PATCH", "DELETE");
        $requestTypeUpper = strtoupper($requestType);
        if (!in_array($requestTypeUpper, $allowedRequestTypes))
        {
            throw new \Exception("API request needs to be one of GET, POST, PUT, PATCH, or DELETE.");
        }
        if ($requestType === "GET")
        {
            $ret = self::sendGetRequest($url, $parameters);
        }
        else 
        {
            $query_string = http_build_query($parameters, '', '&');
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            switch ($requestTypeUpper)
            {
                case "POST":
                {
                    curl_setopt($ch, CURLOPT_POST, 1);
                }
                break;
                case "PUT":
                case "PATCH":
                case "DELETE":
                {
                    curl_setopt($this->m_ch, CURLOPT_CUSTOMREQUEST, $requestTypeUpper);
                }
                break;
                default:
                {
                    throw new \Exception("Unrecognized request type.");
                }
            }
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
            if( defined("CURL_SSL_CERTIFICATE") && is_file(CURL_SSL_CERTIFICATE)) {
                curl_setopt($ch, CURLOPT_CAINFO, CURL_SSL_CERTIFICATE);
                curl_setopt($ch, CURLOPT_CAPATH, CURL_SSL_CERTIFICATE);
            }
            else {
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            }
            if (count($headers) > 0)
            {
                $headersStrings = array();
                foreach ($headers as $key=>$value)
                {
                    $headersStrings[] = "{$key}: {$value}";
                }
                curl_setopt($this->m_ch, CURLOPT_HTTPHEADER, $this->m_headers);
            }
            $jsondata = curl_exec($ch);
            if (curl_error($ch))
            {
                $errMsg = "Connection Error: " . curl_errno($ch) . 
                          ' - ' . curl_error($ch);
                throw new \Exception($errMsg);
            }
            curl_close($ch);
            $ret = json_decode($jsondata); 
            if ($ret == null)
            {
                $errMsg = 'Recieved a non json response from API: ' . $jsondata;
                throw new \Exception($errMsg);
            }
        }
        return $ret;
    }
public static function sendApiRequest ( $ url , array $ parameters , $ requestType = " POST " , $ headers = array ( ) ) { $ allowedRequestTypes = array ( " GET " , " POST " , " PUT " , " PATCH " , " DELETE " ) ; $ requestTypeUpper = strtoupper ( $ requestType ) ; if ( ! in_array ( $ requestTypeUpper , $ allowedRequestTypes ) ) { throw new \ Exception ( " API request needs to be one of GET, POST, PUT, PATCH, or DELETE. " ) ; } if ( $ requestType === " GET " ) { $ ret = self :: sendGetRequest ( $ url , $ parameters ) ; } else { $ query_string = http_build_query ( $ parameters , '' , '&' ) ; $ ch = curl_init ( ) ; curl_setopt ( $ ch , CURLOPT_URL , $ url ) ; switch ( $ requestTypeUpper ) { case " POST " : { curl_setopt ( $ ch , CURLOPT_POST , 1 ) ; } break ; case " PUT " : case " PATCH " : case " DELETE " : { curl_setopt ( $ this -> m_ch , CURLOPT_CUSTOMREQUEST , $ requestTypeUpper ) ; } break ; default : { throw new \ Exception ( " Unrecognized request type. " ) ; } } curl_setopt ( $ ch , CURLOPT_TIMEOUT , 30 ) ; curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , 1 ) ; curl_setopt ( $ ch , CURLOPT_POSTFIELDS , $ query_string ) ; if ( defined ( " CURL_SSL_CERTIFICATE " ) && is_file ( CURL_SSL_CERTIFICATE ) ) { curl_setopt ( $ ch , CURLOPT_CAINFO , CURL_SSL_CERTIFICATE ) ; curl_setopt ( $ ch , CURLOPT_CAPATH , CURL_SSL_CERTIFICATE ) ; } else { curl_setopt ( $ ch , CURLOPT_SSL_VERIFYHOST , 0 ) ; curl_setopt ( $ ch , CURLOPT_SSL_VERIFYPEER , 0 ) ; } if ( count ( $ headers ) > 0 ) { $ headersStrings = array ( ) ; foreach ( $ headers as $ key => $ value ) { $ headersStrings [ ] = " { $ key } :  { $ value } " ; } curl_setopt ( $ this -> m_ch , CURLOPT_HTTPHEADER , $ this -> m_headers ) ; } $ jsondata = curl_exec ( $ ch ) ; if ( curl_error ( $ ch ) ) { $ errMsg = " Connection Error:  " . curl_errno ( $ ch ) . ' - ' . curl_error ( $ ch ) ; throw new \ Exception ( $ errMsg ) ; } curl_close ( $ ch ) ; $ ret = json_decode ( $ jsondata ) ; if ( $ ret == null ) { $ errMsg = 'Recieved a non json response from API: ' . $ jsondata ; throw new \ Exception ( $ errMsg ) ; } } return $ ret ; }
Sends an api request through the use of CURL
 @param string url - the url where the api is located.
Sends an api request through the use of CURL @param string url - the url where the api is located .
// JBB - adding switch to check for a constant in the php bootstrap // which defines the location of a cURL SSL certificate // JBB - these turn off all SSL security checking // @TODO - S.P. to review... // JBB - this is what should happen in this else statement.... // throw new \Exception('cURL SSL certificate not found'); // Manage if user provided headers. # Decode JSON String