/lib/http/HTTPClient.php
PHP | 253 lines | 137 code | 61 blank | 55 comment | 8 complexity | a82c1b9267c6d6ce3ad138dabfa7cdc4 MD5 | raw file
Possible License(s): LGPL-2.1
- <?php
- namespace c3s\lib\http;
-
- /**
- *
- * @author admin
- *
- */
- class HTTPClient {
-
- const METHOD_GET = 'GET';
- const METHOD_PUT = 'PUT';
- const METHOD_HEADER = 'HEADER';
- const METHOD_POST = 'POST';
- const METHOD_DELETE = 'DELETE';
-
- private $curl = null;
-
- public function __construct() {
-
- }
-
-
- /**
- * @var string
- */
- private $method = 'GET';
-
- /**
- * @return the $method
- */
-
- public function getMethod() {
- return $this->method;
- }
-
- /**
- * @param string $method
- */
- public function setMethod($method) {
- $this->method = $method;
- }
-
-
- private $headers = array();
-
- /**
- * @return the $headers
- */
- public function getHeaders() {
- return $this->headers;
- }
-
- /**
- * @param multitype: $headers
- */
- public function setHeaders($headers) {
- $this->headers = $headers;
- }
-
- /**
- * @param array|string $header
- */
- public function addHeader($header) {
- if (is_array($header)) {
- $this->headers = array_merge($this->headers, $header);
- } else {
- $this->headers[] = $header;
- }
- }
-
-
- /**
- * @var bool
- */
- private $responseHeader = false;
-
- /**
- * @return the $responseHeader
- */
- public function getResponseHeader() {
- return $this->responseHeader;
- }
-
- /**
- * @param boolean $responseHeader
- */
- public function setResponseHeader($responseHeader) {
- $this->responseHeader = $responseHeader?true:false;
- }
-
-
- /**
- * @var bool
- */
- private $autoRedirect = true;
-
-
- /**
- * @return the $autoRedirect
- */
- public function getAutoRedirect() {
- return $this->autoRedirect;
- }
-
- /**
- * @param boolean $autoRedirect
- */
- public function setAutoRedirect($autoRedirect) {
- $this->autoRedirect = $autoRedirect?true:false;
- }
-
-
- /**
- * @var string
- */
- private $content = '';
-
-
- /**
- * @return the $content
- */
- public function getContent() {
- return $this->content;
- }
-
- /**
- * @param string $content
- */
- public function setContent($content) {
- $this->content = $content;
- }
-
-
- private $otherOptions = array();
-
- public function setOption($curl_option, $value) {
- $this->otherOptions[$curl_option] = $value;
- //curl_setopt($this->curl, $curl_option, $value);
- }
-
-
- private $debugOutput = false;
- private $file = false;
-
- public function debug($filename) {
- $this->debugOutput = $filename;
- }
-
-
- private $cookieFile = false;
-
- public function setCookieFile($cookieFile) {
- $this->cookieFile = $cookieFile;
- }
-
- public function deleteCookieFile() {
- @unlink($this->cookieFile);
- }
-
- protected function makeCurlOptions() {
-
- $this->curl = curl_init();
-
- curl_setopt ($this->curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt ($this->curl, CURLOPT_FOLLOWLOCATION, $this->autoRedirect?1:0);
- // NOT CHECK SSL
- curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0);
- //
- if ($this->headers && count($this->headers)) {
- curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->headers);
- }
- // clear special method headers
- curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "GET");
- curl_setopt($this->curl, CURLOPT_POST, 0);
-
- foreach ($this->otherOptions as $curl_option => $value) {
- curl_setopt($this->curl, $curl_option, $value);
- }
-
- if ($this->cookieFile) {
- curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookieFile); //initiates cookie file if needed
- curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookieFile); // Uses cookies from previous session if exist
- }
-
- if ($this->debugOutput) {
- $this->file = fopen($this->debugOutput, 'w+');
- curl_setopt($this->curl, CURLOPT_HEADER, 1);
- curl_setopt($this->curl, CURLOPT_FILE, $this->file);
-
- curl_setopt($this->curl, CURLOPT_VERBOSE, true);
- //$verbose = fopen('php://temp', 'w+');
- curl_setopt($this->curl, CURLOPT_STDERR, $this->file);
-
- }
- }
-
- public function request($method, $url, $body = '', $headers = array()) {
-
- $this->method = $method;
- $old_headers = $this->headers;
- $this->addHeader($headers);
-
- $this->makeCurlOptions();
-
- if ($this->debugOutput) {
- fputs($this->file, is_array($body)?json_encode($body):$body);
- }
-
- switch ($this->method) {
- case self::METHOD_GET:
- curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "GET");
- break;
- case self::METHOD_HEADER:
- curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "HEADER");
- break;
- case self::METHOD_POST:
- curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "POST");
- curl_setopt($this->curl, CURLOPT_POST, 1);
- curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
- break;
- case self::METHOD_PUT:
- curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "PUT");
- curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
- break;
- case self::METHOD_DELETE:
- curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "DELETE");
- curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
- break;
- }
-
- curl_setopt($this->curl, CURLOPT_URL, $url);
-
-
- $this->content = curl_exec($this->curl);
-
- $this->headers = $old_headers;
-
- return $this->content;
- }
-
- public function getError() {
- return curl_error($this->curl);
- }
-
- public function getErrorCode() {
- return curl_errno($this->curl);
- }
-
- }
-
- ?>