From 867cf516566842d455794330a4547d0f862c41fe Mon Sep 17 00:00:00 2001 From: Steven Rosato Date: Thu, 26 Mar 2020 18:51:06 -0400 Subject: [PATCH 1/2] Fixes trying to access array offset on value of type resource on PHP 7.4 --- .../communications/HttpConnector.php | 63 ++++++++++--------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/src/Beanstream/communications/HttpConnector.php b/src/Beanstream/communications/HttpConnector.php index bd349fd..4f9a2ca 100644 --- a/src/Beanstream/communications/HttpConnector.php +++ b/src/Beanstream/communications/HttpConnector.php @@ -2,84 +2,84 @@ /** * HTTPConnector class to handle HTTP requests to the REST API - * + * * @author Kevin Saliba */ class HttpConnector { /** * Base64 Encoded Auth String - * + * * @var string $_auth */ protected $_auth; - + /** * Constructor - * + * * @param string $auth base64 encoded string to assign to the http header - */ + */ function __construct($auth) { //set auth for this connection only $this->_auth = $auth; } - - + + /** * processTransaction() function - Public facing function to send a request to an endpoint. - * + * * @param string $http_method HTTP method to use (defaults to GET if $data==null; defaults to PUT if $data!=null) * @param string $endpoint Incoming API Endpoint * @param array $data Data for POST requests, not needed for GETs * @access public * @return array Parsed API response from private request method - * + * */ public function processTransaction($http_method, $endpoint, $data) { //call internal request function return $this->request($http_method, $endpoint, $data); } - - + + /** * request() function - Internal function to send a request to an endpoint. - * + * * @param string|null $http_method HTTP method to use (defaults to GET if $data==null; defaults to PUT if $data!=null) * @param string $url Incoming API Endpoint * @param array|null $data Data for POST requests, not needed for GETs * @access private * @return array Parsed API response - * + * * @throws ApiException * @throws ConnectorException */ private function request($http_method = NULL, $url, $data = NULL) { - //check to see if we have curl installed on the server + //check to see if we have curl installed on the server if ( ! extension_loaded('curl')) { //no curl throw new ConnectorException('The cURL extension is required', 0); } - + //init the curl request //via endpoint to curl $req = curl_init($url); - + //set request headers with encoded auth curl_setopt($req, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Authorization: Passcode '.$this->_auth, )); - - //set other curl options + + //set other curl options curl_setopt($req, CURLOPT_RETURNTRANSFER, true); curl_setopt($req, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($req, CURLOPT_TIMEOUT, 30); - + //test ssl3 (remember to set platform to 'ssltest') //should no longer work after 01/01/2015 //curl_setopt($req, CURLOPT_SSLVERSION, 3); - + //set http method //default to GET if data is null //default to POST if data is not null @@ -90,35 +90,36 @@ private function request($http_method = NULL, $url, $data = NULL) $http_method = 'POST'; } } - + //set http method in curl curl_setopt($req, CURLOPT_CUSTOMREQUEST, $http_method); - + //make sure incoming payload is good to go, set it if ( ! is_null($data)) { curl_setopt($req, CURLOPT_POSTFIELDS, json_encode($data)); } - + //execute curl request $raw = curl_exec($req); - + if (false === $raw) { //make sure we got something back throw new ConnectorException(curl_error($req), -curl_errno($req)); } - + //decode the result $res = json_decode($raw, true); if (is_null($res)) { //make sure the result is good to go throw new ConnectorException('Unexpected response format', 0); } - - //check for return errors from the API - if (isset($res['code']) && 1 < $res['code'] && !($req['http_code'] >= 200 && $req['http_code'] < 300)) { + + //check for return errors from the API + $httpCode = curl_getinfo($req, CURLINFO_HTTP_CODE); + if (isset($res['code']) && 1 < $res['code'] && !($httpCode >= 200 && $httpCode < 300)) { throw new ApiException($res['message'], $res['code']); } - + return $res; - } - + } + } From 7f3162c4ae11574688ca2ec81f68400ec13c8181 Mon Sep 17 00:00:00 2001 From: Steven Rosato Date: Wed, 20 Jul 2022 14:16:20 -0400 Subject: [PATCH 2/2] Apply fix again after merging master --- src/Beanstream/communications/HttpConnector.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Beanstream/communications/HttpConnector.php b/src/Beanstream/communications/HttpConnector.php index f0d0989..2ec79ff 100644 --- a/src/Beanstream/communications/HttpConnector.php +++ b/src/Beanstream/communications/HttpConnector.php @@ -112,11 +112,12 @@ private function request($http_method = NULL, $url, $data = NULL) if (is_null($res)) { //make sure the result is good to go throw new ConnectorException('Unexpected response format', 0); } - - //check for return errors from the API - if (isset($res['code']) && 1 < $res['code'] && !($req['http_code'] >= 200 && $req['http_code'] < 300)) { + + //check for return errors from the API + $httpCode = curl_getinfo($req, CURLINFO_HTTP_CODE); + if (isset($res['code']) && 1 < $res['code'] && !($httpCode >= 200 && $httpCode < 300)) { throw new ApiException($res['message'], $res['code']); - } + } return $res; }