/** * Execute the call to the webservice * @return RestClient */ public function execute() { if($this->method === "POST") { curl_setopt($this->curl,CURLOPT_POST,true); curl_setopt($this->curl,CURLOPT_POSTFIELDS,$this->params); } else if($this->method == "GET"){ curl_setopt($this->curl,CURLOPT_HTTPGET,true); $this->treatURL(); } else if($this->method === "PUT") { curl_setopt($this->curl,CURLOPT_PUT,true); $this->treatURL(); $this->file = tmpFile(); fwrite($this->file,$this->params); fseek($this->file,0); curl_setopt($this->curl,CURLOPT_INFILE,$this->file); curl_setopt($this->curl,CURLOPT_INFILESIZE,strlen($this->params)); } else { curl_setopt($this->curl,CURLOPT_CUSTOMREQUEST,$this->method); } $requestHeaders = $this->requestHeaders; if($this->contentType != null) { $requestHeaders = array_merge(array("Content-Type: ".$this->contentType), $this->requestHeaders); } curl_setopt($this->curl,CURLOPT_HTTPHEADER,$requestHeaders); curl_setopt($this->curl, CURLINFO_HEADER_OUT, TRUE); curl_setopt($this->curl,CURLOPT_URL,$this->url); $r = curl_exec($this->curl); if (($errno = curl_errno($this->curl))) { throw new Exception(curl_error($this->curl), $errno); } $this->treatResponse($r); // Extract the headers and response return $this ; }
/** * Execute the call to the webservice * @return RestClient */ public function execute() { if ($this->method === "POST") { curl_setopt($this->curl, CURLOPT_POST, true); curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->params); } else { if ($this->method == "GET") { curl_setopt($this->curl, CURLOPT_HTTPGET, true); $this->treatURL(); } else { if ($this->method === "PUT") { curl_setopt($this->curl, CURLOPT_PUT, true); $this->treatURL(); $this->file = tmpFile(); fwrite($this->file, $this->params); fseek($this->file, 0); curl_setopt($this->curl, CURLOPT_INFILE, $this->file); curl_setopt($this->curl, CURLOPT_INFILESIZE, strlen($this->params)); } else { curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $this->method); } } } if ($this->contentType != null) { curl_setopt($this->curl, CURLOPT_HTTPHEADER, array("Content-Type: " . $this->contentType)); } curl_setopt($this->curl, CURLOPT_URL, $this->url); $r = curl_exec($this->curl); $this->treatResponse($r); // Extract the headers and response return $this; }
/** * Execute the call to the webservice * @return RestClient */ public function execute($headerParams) { if ($this->method === "POST") { curl_setopt($this->curl, CURLOPT_POST, true); curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->params); if ($this->postBody) { curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->postBody); } $this->treatURL(); } else { if ($this->method == "GET") { curl_setopt($this->curl, CURLOPT_HTTPGET, true); $this->treatURL(); } else { if ($this->method === "PUT") { curl_setopt($this->curl, CURLOPT_PUT, true); if ($this->postBody) { curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->postBody); } $this->treatURL(); $this->file = tmpFile(); fwrite($this->file, $this->postBody); fseek($this->file, 0); curl_setopt($this->curl, CURLOPT_INFILE, $this->file); curl_setopt($this->curl, CURLOPT_INFILESIZE, strlen($this->postBody)); } else { curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $this->method); $this->treatURL(); } } } if ($this->contentType != null) { $headers = array(); foreach ($headerParams as $key => $val) { array_push($headers, $key . ": " . $val); // print $key ."=". $val ."\n"; } array_push($headers, "Content-Type: " . $this->contentType); array_push($headers, "Accept: " . $this->accept); //curl_setopt($this->curl, CURLOPT_HTTPHEADER, array("Content-Type: " . $this->contentType, "Accept: " . $this->accept, "sessionId: " . $this->sessionId, "adminKey: " . $this->adminKey, "deletePermanent: " . $this->deletePermanent)); curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers); } curl_setopt($this->curl, CURLOPT_URL, $this->url); $r = curl_exec($this->curl); $this->treatResponse($r); // Extract the headers and response return $this; }
/** * Execute calls * @param $url String * @param $method String * @param $postFields String * @param $username String * @param $password String * @param $contentType String * @return RESTClient */ public function call($url, $method, $postFields = null, $username = null, $password = null, $contentType = null) { if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0 && !empty($this->format)) { $url = "{$this->api_url}{$url}.{$this->format}"; } $this->http_url = $url; $this->contentType = $contentType; $this->postFields = $postFields; $url = in_array($method, self::$paramsOnUrlMethod) ? $this->to_url() : $this->get_http_url(); is_object($this->ch) or $this->__construct(); switch ($method) { case 'POST': curl_setopt($this->ch, CURLOPT_POST, TRUE); if ($this->postFields != null) { curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->postFields); } break; case 'DELETE': curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); break; case 'PUT': curl_setopt($this->ch, CURLOPT_PUT, TRUE); if ($this->postFields != null) { $this->file = tmpFile(); fwrite($this->file, $this->postFields); fseek($this->file, 0); curl_setopt($this->ch, CURLOPT_INFILE, $this->file); curl_setopt($this->ch, CURLOPT_INFILESIZE, strlen($this->postFields)); } break; } $this->setAuthorizeInfo($username, $password); $this->contentType != null && curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('Content-type:' . $this->contentType)); curl_setopt($this->ch, CURLOPT_URL, $url); $response = curl_exec($this->ch); $this->http_code = curl_getinfo($this->ch, CURLINFO_HTTP_CODE); $this->http_info = array_merge($this->http_info, curl_getinfo($this->ch)); $this->close(); return $response; }
/** * Make an HTTP request * * @return API results */ public function http($url, $method, $postfields = NULL) { $this->http_info = array(); $ci = curl_init(); /* Curl settings */ curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout); curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout); curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:')); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer); curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader')); curl_setopt($ci, CURLOPT_HEADER, FALSE); switch ($method) { case 'POST': curl_setopt($ci, CURLOPT_POST, TRUE); if (!empty($postfields)) { curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); } break; case 'DELETE': curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE'); if (!empty($postfields)) { $url = "{$url}?{$postfields}"; } break; case 'PUT': #Added by Edison tsai on 15:16 2010/08/27 for PUT method curl_setopt($ci, CURLOPT_PUT, TRUE); if (!empty($postfields)) { $this->file = tmpFile(); fwrite($this->file, $postfields); rewind($this->file); curl_setopt($ci, CURLOPT_INFILE, $this->file); curl_setopt($ci, CURLOPT_INFILESIZE, strlen($postfields)); //curl_setopt($ci, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); } break; case 'UPFILE': #Added by Edison tsai on 18:23 2010/09/15 for upload file method curl_setopt($ci, CURLOPT_POST, TRUE); if (!empty($postfields) && is_array($this->fileInfo) && count($this->fileInfo) >= 3) { $postArray = OAuthUtil::parse_parameters($postfields); if (is_array($postArray) && count($postArray) > 0) { $this->getBoundary(); $multiPart = $this->getMidBoundary() . self::$CRLF; $multiPart .= 'Content-Disposition: form-data; name="productImage";filename="' . $this->fileInfo['filename'] . '"' . self::$CRLF . 'Content-Type: ' . $this->fileInfo['filetype'] . self::$CRLF . self::$CRLF . $this->fileInfo['filedata'] . self::$CRLF; foreach ($postArray as $pk => $pa) { $multiPart .= $this->getMidBoundary() . self::$CRLF . 'Content-Disposition: form-data;name="' . $pk . '"' . self::$CRLF . self::$CRLF . $pa . self::$CRLF; } #end foreach //$multiPart .= self::$CRLF.$this->getEndBoundary(); } #end if curl_setopt($ci, CURLOPT_POSTFIELDS, $multiPart); } curl_setopt($ci, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data; boundary=' . $this->boundary)); $this->fileInfo = array(); break; } curl_setopt($ci, CURLOPT_URL, $url); $response = curl_exec($ci); $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE); $this->http_info = array_merge($this->http_info, curl_getinfo($ci)); $this->url = $url; curl_close($ci); is_object($this->file) && fclose($this->file); return $response; }