Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG Don't ignore curl errors #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 48 additions & 38 deletions src/crm/utility/ZohoHTTPConnector.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace zcrmsdk\crm\utility;

use Exception;

/**
* Purpose of this class is to trigger API call and fetch the response
*
Expand All @@ -9,50 +11,50 @@
*/
class ZohoHTTPConnector
{

private $url = null;

private $requestParams = array();

private $requestHeaders = array();

private $requestParamCount = 0;

private $requestBody;

private $requestType = APIConstants::REQUEST_METHOD_GET;

private $userAgent = "ZohoCRM PHP SDK";

private $apiKey = null;

private $isBulkRequest = false;

private function __construct()
{}

public static function getInstance()
{
return new ZohoHTTPConnector();
}

public function fireRequest()
{
$curl_pointer = curl_init();
if (is_array(self::getRequestParamsMap()) && count(self::getRequestParamsMap()) > 0) {
$url = self::getUrl() . "?" . self::getUrlParamsAsString(self::getRequestParamsMap());
curl_setopt($curl_pointer, CURLOPT_URL, $url);

} else {
curl_setopt($curl_pointer, CURLOPT_URL, self::getUrl());

}
curl_setopt($curl_pointer, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_pointer, CURLOPT_HEADER, 1);
curl_setopt($curl_pointer, CURLOPT_USERAGENT, $this->userAgent);
curl_setopt($curl_pointer, CURLOPT_HTTPHEADER, self::getRequestHeadersAsArray());
curl_setopt($curl_pointer, CURLOPT_CUSTOMREQUEST, APIConstants::REQUEST_METHOD_GET);

if ($this->requestType === APIConstants::REQUEST_METHOD_POST) {
curl_setopt($curl_pointer, CURLOPT_CUSTOMREQUEST, APIConstants::REQUEST_METHOD_POST);
curl_setopt($curl_pointer, CURLOPT_POST, true);
Expand All @@ -64,15 +66,19 @@ public function fireRequest()
curl_setopt($curl_pointer, CURLOPT_CUSTOMREQUEST, APIConstants::REQUEST_METHOD_DELETE);
}
$result = curl_exec($curl_pointer);
if ($result === false) {
throw new Exception(curl_error($curl_pointer), curl_errno($curl_pointer));
}

$responseInfo = curl_getinfo($curl_pointer);
curl_close($curl_pointer);

return array(
$result,
$responseInfo
);
}

public function downloadFile()
{
$curl_pointer = curl_init();
Expand All @@ -83,24 +89,28 @@ public function downloadFile()
curl_setopt($curl_pointer, CURLOPT_HTTPHEADER, self::getRequestHeadersAsArray());
// curl_setopt($curl_pointer,CURLOPT_SSLVERSION,3);
$result = curl_exec($curl_pointer);
if ($result === false) {
throw new Exception(curl_error($curl_pointer), curl_errno($curl_pointer));
}

$responseInfo = curl_getinfo($curl_pointer);
curl_close($curl_pointer);
return array(
$result,
$responseInfo
);
}

public function getUrl()
{
return $this->url;
}

public function setUrl($url)
{
$this->url = $url;
}

public function addParam($key, $value)
{
if ($this->requestParams[$key] == null) {
Expand All @@ -113,7 +123,7 @@ public function addParam($key, $value)
$this->requestParams[$key] = $valArray;
}
}

public function addHeader($key, $value)
{
if ($this->requestHeaders[$key] == null) {
Expand All @@ -126,7 +136,7 @@ public function addHeader($key, $value)
$this->requestHeaders[$key] = $valArray;
}
}

public function getUrlParamsAsString($urlParams)
{
$params_as_string = "";
Expand All @@ -138,61 +148,61 @@ public function getUrlParamsAsString($urlParams)
}
$params_as_string = rtrim($params_as_string, "&");
$params_as_string = str_replace(PHP_EOL, '', $params_as_string);

return $params_as_string;
}

public function setRequestHeadersMap($headers)
{
$this->requestHeaders = $headers;
}

public function getRequestHeadersMap()
{
return $this->requestHeaders;
}

public function setRequestParamsMap($params)
{
$this->requestParams = $params;
}

public function getRequestParamsMap()
{
return $this->requestParams;
}

public function setRequestBody($reqBody)
{
$this->requestBody = $reqBody;
}

public function getRequestBody()
{
return $this->requestBody;
}

public function setRequestType($reqType)
{
$this->requestType = $reqType;
}

public function getRequestType()
{
return $this->requestType;
}

public function getRequestHeadersAsArray()
{
$headersArray = array();
$headersMap = self::getRequestHeadersMap();
foreach ($headersMap as $key => $value) {
$headersArray[] = $key . ":" . $value;
}

return $headersArray;
}

/**
* Get the API Key used in the input json data(like 'modules', 'data','layouts',..etc)
*
Expand All @@ -202,7 +212,7 @@ public function getApiKey()
{
return $this->apiKey;
}

/**
* Set the API Key used in the input json data(like 'modules', 'data','layouts',..etc)
*
Expand All @@ -212,7 +222,7 @@ public function setApiKey($apiKey)
{
$this->apiKey = $apiKey;
}

/**
* isBulkRequest
*
Expand All @@ -222,7 +232,7 @@ public function isBulkRequest()
{
return $this->isBulkRequest;
}

/**
* isBulkRequest
*
Expand All @@ -233,4 +243,4 @@ public function setBulkRequest($isBulkRequest)
{
$this->isBulkRequest = $isBulkRequest;
}
}
}