/**
  * Use the send method to call every endpoint except for oauth/tokens
  *
  * @param Client $client
  * @param string $endPoint
  * @param mixed  $json
  * @param string $method
  * @param string $contentType
  *
  * @throws \Exception
  *
  * @return mixed
  */
 public static function send(Client $client, $endPoint, $json = null, $method = 'GET', $contentType = 'application/json')
 {
     $url = $client->getApiUrl() . $endPoint;
     $method = strtoupper($method);
     if (null == $json) {
         $json = new \stdClass();
     } else {
         if ($contentType == 'application/json' && $method != 'GET' && $method != 'DELETE') {
             $json = json_encode($json);
         }
     }
     if ($method == 'POST') {
         $curl = curl_init($url);
         curl_setopt($curl, CURLOPT_POST, true);
         curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
         if (is_array($json) && (isset($json['filename']) || isset($json['uploaded_data']))) {
             $filename = isset($json['filename']) ? $json['filename'] : $json['uploaded_data'];
             $file = fopen($filename, 'r');
             $size = filesize($filename);
             $fileData = fread($file, $size);
             curl_setopt($curl, CURLOPT_POSTFIELDS, $fileData);
             curl_setopt($curl, CURLOPT_INFILE, $file);
             curl_setopt($curl, CURLOPT_INFILESIZE, $size);
             curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/binary'));
         }
     } else {
         if ($method == 'PUT') {
             $curl = curl_init($url);
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
             curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
         } else {
             $curl = curl_init($url . ($json != (object) null ? (strpos($url, '?') === false ? '?' : '&') . http_build_query($json) : ''));
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method ? $method : 'GET');
         }
     }
     if ($client->getAuthType() == 'oauth_token') {
         curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: ' . $contentType, 'Accept: application/json', 'Authorization: Bearer ' . $client->getAuthText()));
     } else {
         curl_setopt($curl, CURLOPT_USERPWD, $client->getAuthText());
         curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: ' . $contentType, 'Accept: application/json'));
     }
     curl_setopt($curl, CURLINFO_HEADER_OUT, true);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
     curl_setopt($curl, CURLOPT_TIMEOUT, 30);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($curl, CURLOPT_HEADER, true);
     curl_setopt($curl, CURLOPT_VERBOSE, true);
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
     $response = curl_exec($curl);
     if ($response === false) {
         throw new \Exception('No response from curl_exec in ' . __METHOD__);
     }
     $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
     $responseBody = substr($response, $headerSize);
     $client->setDebug(curl_getinfo($curl, CURLINFO_HEADER_OUT), curl_getinfo($curl, CURLINFO_HTTP_CODE), substr($response, 0, $headerSize));
     curl_close($curl);
     return json_decode($responseBody);
 }
Example #2
0
File: Http.php Project: KF5/PHP-SDK
 public static function send(Client $client, $endPoint, $data = null, $method = 'GET', $contentType = 'application/json')
 {
     $url = $client->getApiUrl() . $endPoint;
     $method = strtoupper($method);
     if (null == $data) {
         $data = new stdClass();
     } else {
         if ($contentType == 'application/json' && $method != 'GET' && $method != 'DELETE') {
             $data = json_encode($data);
         }
     }
     if ($method == 'POST') {
         $curl = curl_init($url);
         curl_setopt($curl, CURLOPT_POST, true);
         curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
         if (is_array($data)) {
             if (isset($data['upload'])) {
                 $filename = $data['upload'];
                 $file = fopen($filename, 'r');
                 $size = filesize($filename);
                 $fileData = fread($file, $size);
                 curl_setopt($curl, CURLOPT_POSTFIELDS, $fileData);
                 curl_setopt($curl, CURLOPT_INFILE, $file);
                 curl_setopt($curl, CURLOPT_INFILESIZE, $size);
             }
         }
     } else {
         if ($method == 'PUT') {
             $curl = curl_init($url);
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
         } else {
             $url = $url . ($data != null ? (strpos($url, '?') === false ? '?' : '&') . http_build_query($data) : '');
             $curl = curl_init($url);
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method ? $method : 'GET');
         }
     }
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLOPT_USERPWD, $client->getAuthText());
     curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: ' . $contentType, 'Accept: application/json'));
     curl_setopt($curl, CURLINFO_HEADER_OUT, true);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
     curl_setopt($curl, CURLOPT_TIMEOUT, 30);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($curl, CURLOPT_HEADER, true);
     curl_setopt($curl, CURLOPT_VERBOSE, true);
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
     $response = curl_exec($curl);
     if ($response === false) {
         throw new Exception(sprintf('Curl error message: "%s" in %s', curl_error($curl), __METHOD__));
     }
     $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
     $responseBody = substr($response, $headerSize);
     $client->setDebug(curl_getinfo($curl, CURLINFO_HEADER_OUT), curl_getinfo($curl, CURLINFO_HTTP_CODE), substr($response, 0, $headerSize), $responseBody);
     curl_close($curl);
     return json_decode($responseBody);
 }
 /**
  * Use the send method to call every endpoint except for oauth/tokens
  *
  * @param Client $client
  * @param string $endPoint
  * @param array $json
  * @param string $method
  * @param string $contentType
  *
  * @throws \Exception
  *
  * @return mixed
  */
 public static function send(Client $client, $endPoint, $json = array(), $method = 'GET', $contentType = 'application/json')
 {
     $url = $client->getApiUrl() . $endPoint;
     $method = strtoupper($method);
     $curl = isset(self::$curl) ? self::$curl : new CurlRequest();
     $curl->setopt(CURLOPT_URL, $url);
     if ($method === 'POST') {
         $curl->setopt(CURLOPT_POST, true);
     } else {
         if ($method === 'PUT') {
             $curl->setopt(CURLOPT_CUSTOMREQUEST, 'PUT');
         } else {
             $st = http_build_query((array) $json);
             $curl->setopt(CURLOPT_URL, $url . ($st !== array() ? (strpos($url, '?') === false ? '?' : '&') . $st : ''));
             $curl->setopt(CURLOPT_CUSTOMREQUEST, $method);
         }
     }
     $httpHeader = array('Accept: application/json');
     if ($client->getAuthType() == 'oauth_token') {
         $httpHeader[] = 'Authorization: Bearer ' . $client->getAuthText();
     } else {
         $curl->setopt(CURLOPT_USERPWD, $client->getAuthText());
     }
     /* DO NOT SET CONTENT TYPE IF UPLOADING */
     if (!isset($json['uploaded_data'])) {
         if (isset($json['filename'])) {
             $filename = $json['filename'];
             $file = fopen($filename, 'r');
             $size = filesize($filename);
             $fileData = fread($file, $size);
             $json = $fileData;
             $curl->setopt(CURLOPT_INFILE, $file);
             $curl->setopt(CURLOPT_INFILESIZE, $size);
         } else {
             if (isset($json['body'])) {
                 $curl->setopt(CURLOPT_INFILESIZE, strlen($json['body']));
                 $json = $json['body'];
             }
         }
         $httpHeader[] = 'Content-Type: ' . $contentType;
     } else {
         $contentType = '';
     }
     if ($contentType === 'application/json') {
         $json = json_encode($json);
     }
     $curl->setopt(CURLOPT_POSTFIELDS, $json);
     $curl->setopt(CURLOPT_HTTPHEADER, $httpHeader);
     $curl->setopt(CURLINFO_HEADER_OUT, true);
     $curl->setopt(CURLOPT_RETURNTRANSFER, true);
     $curl->setopt(CURLOPT_CONNECTTIMEOUT, 30);
     $curl->setopt(CURLOPT_TIMEOUT, 30);
     $curl->setopt(CURLOPT_SSL_VERIFYPEER, false);
     $curl->setopt(CURLOPT_HEADER, true);
     $curl->setopt(CURLOPT_VERBOSE, true);
     $curl->setopt(CURLOPT_FOLLOWLOCATION, true);
     $curl->setopt(CURLOPT_MAXREDIRS, 3);
     $response = $curl->exec();
     if ($response === false) {
         throw new \Exception(sprintf('Curl error message: "%s" in %s', $curl->error(), __METHOD__));
     }
     $headerSize = $curl->getinfo(CURLINFO_HEADER_SIZE);
     $responseBody = substr($response, $headerSize);
     $responseObject = json_decode($responseBody);
     $client->setDebug($curl->getinfo(CURLINFO_HEADER_OUT), $curl->getinfo(CURLINFO_HTTP_CODE), substr($response, 0, $headerSize), isset($responseObject->error) ? $responseObject : null);
     $responseCode = $client->getDebug()->lastResponseCode;
     if ($responseCode >= 400) {
         print $client->getDebug();
         throw new ResponseException(__METHOD__);
     }
     $curl->close();
     self::$curl = null;
     return $responseObject;
 }