Sorry for my English, I'm not a native speaker so please don't minus me too much. I'm working on Woleet Blockchain API to protect intellectual properties (eg. Files, Docs, Images and Videos). I'm using PHP cURL library to make api requests to server. When I used PUT request with cURL it states the following ERROR.
{"apiException":"Required request body is missing: public io.woleet.service.dto.AnchorDTO io.woleet.controller.AnchorController.anchorPut(io.woleet.gateway.WoleetUserDetails,java.lang.String,java.util.Map<java.lang.String, java.lang.Object>) throws io.woleet.exception.ApiException"}
Any help would be appreciated. Here is the link to api Woleet Documentation documentation in case you need to study about it. Because there is no support for this on the documentation so I decided to put it on stackoverflow if anyone had faced a same problem. My current PHP code looks like this.
PHP cURL Handler Class:
class CurlHandler {
private $_url;
private $_response;
private $_includeHeader;
private $_noBody;
private $_status;
private $_binaryTransfer;
private $_curl_error;
private $_curl_info;
private $authentication = 0;
private $auth_name = '';
private $auth_pass = '';
private $_header_accept_encoding="";
private $_header_accept_language="";
private $_header_accept="";
private $_header_content_type = "";
private $_header_referer = "";
private $_header_cache_control = "";
private $_header_user_agent ="";
private $_header_host = "";
private $headers = [];
public function useAuth($use){
$this->authentication = 0;
if($use == true) $this->authentication = 1;
}
/**
* @return array
*/
public function getHeaders() {
return $this->headers;
}
public function __construct($url) {
$this->_url = $url;
$this->_header_accept_encoding="Accept-Encoding: gzip, deflate";
$this->_header_accept_language="Accept-Language: en-US,en;q=0.5";
$this->_header_accept="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$this->_header_content_type="Content-Type: application/x-www-form-urlencoded; charset=utf-8";
$this->_header_referer = "Referer: http://www.blockchainhuissieray.com/index.php";
$this->_header_cache_control = "Cache-Control: no-cache";
$this->_header_user_agent = "User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0";
$this->_header_host = "Host: www.example.com";
$this->_curl_error = null;
$this->_curl_info = null;
}
/**
* @param string $auth_name
*/
public function setAuthName($auth_name) {
$this->auth_name = $auth_name;
}
/**
* @param string $auth_pass
*/
public function setAuthPass($auth_pass) {
$this->auth_pass = $auth_pass;
}
/**
* @return mixed
*/
public function getStatus() {
return $this->_status;
}
/**
* @return mixed
*/
public function getResponse() {
return $this->_response;
}
/**
* @return null
*/
public function getCurlError() {
return $this->_curl_error;
}
/**
* @return null
*/
public function getCurlInfo() {
return $this->_curl_info;
}
/**
* @param mixed $includeHeader
*/
public function setIncludeHeader($includeHeader) {
$this->_includeHeader = $includeHeader;
}
/**
* @return mixed
*/
public function getIncludeHeader() {
return $this->_includeHeader;
}
public function makeHTTPHeader($accept=null, $conent_type=null, $encoding=null, $language=null, $referer=null,
$host=null, $agent=null, $cache_control=null){
if($accept!=null){
$this->_header_accept = "Accept: ".$accept;
}
if($encoding!=null){
$this->_header_accept_encoding = "Accept-Encoding: ".$encoding;
$this->headers[] = $this->_header_accept_encoding;
}
if($language!=null){
$this->_header_accept_language = "Accept-Language: ".$language;
$this->headers[] = $this->_header_accept_language;
}
if($conent_type!=null){
$this->_header_content_type = "Content-Type: ".$conent_type;
}
if($referer != null){
$this->_header_referer = "Referer: ".$referer;
$this->headers[] = $this->_header_referer;
}
if($host!=null){
$this->_header_host = "Host: ".$host;
$this->headers[] = $this->_header_host;
}
if($agent!=null){
$this->_header_user_agent = "User-Agent: ".$agent;
$this->headers[] = $this->_header_user_agent;
}
if($cache_control!=null){
$this->_header_cache_control = "Cache-Control:".$cache_control;
$this->headers[] = $this->_header_cache_control;
}
$this->headers[] = $this->_header_accept;
$this->headers[] = $this->_header_content_type;
}
public function extraHeader ($headerValue){
$this->headers[] = $headerValue;
}
public function post($url, $data=null){
$url = $this->_url.$url;
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
if ($data!=null){
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
if($this->authentication){
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $this->auth_name.":".$this->auth_pass);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if($this->_includeHeader){
curl_setopt($curl, CURLOPT_HEADER,true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);
}
curl_setopt($curl, CURLOPT_VERBOSE, 1);
$this->_response = curl_exec($curl);
$this->_curl_error = curl_error($curl);
$this->_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$this->_curl_info = curl_getinfo($curl);
curl_close($curl);
}
public function put($url, $data){
$url = $this->_url.$url;
print($url);
$curl = curl_init();
curl_setopt($curl, CURLOPT_PUT, 1);
if ($data!=null){
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
if($this->authentication){
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $this->auth_name.":".$this->auth_pass);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if($this->_includeHeader){
curl_setopt($curl, CURLOPT_HEADER,true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);
}
curl_setopt($curl, CURLOPT_VERBOSE, 1);
$this->_response = curl_exec($curl);
$this->_curl_error = curl_error($curl);
$this->_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$this->_curl_info = curl_getinfo($curl);
curl_close($curl);
}
public function delete($url, $data){
$url = $this->_url.$url;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
if($data!=null){
$url = sprintf("%s?%s", $url, http_build_query($data));
}
if($this->authentication){
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $this->auth_name.":".$this->auth_pass);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if($this->_includeHeader){
curl_setopt($curl, CURLOPT_HEADER,true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);
}
curl_setopt($curl, CURLOPT_VERBOSE, 1);
$this->_response = curl_exec($curl);
$this->_curl_error = curl_error($curl);
$this->_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$this->_curl_info = curl_getinfo($curl);
curl_close($curl);
}
public function get($url, $data=null){
$url = $this->_url.$url;
$curl = curl_init();
if($data!=null){
$url = sprintf("%s?%s", $url, http_build_query($data));
}
if($this->authentication){
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $this->auth_name.":".$this->auth_pass);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if($this->_includeHeader){
curl_setopt($curl, CURLOPT_HEADER,true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);
}
curl_setopt($curl, CURLOPT_VERBOSE, 1);
$this->_response = curl_exec($curl);
$this->_curl_error = curl_error($curl);
$this->_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$this->_curl_info = curl_getinfo($curl);
curl_close($curl);
}
}
PHP WoleetManager Class:
class WoleetManager {
/*
* This class impliments curl methods to make requests to Woleet API.
*
**/
private $curlHandler = null;
private $base_url = null;
private $auth_username = null;
private $auth_password = null;
public function __construct() {
$this->base_url = "https://api.woleet.io/v1";
$this->curlHandler = new CurlHandler($this->base_url);
$this->auth_username = "something";
$this->auth_password = "something";
$this->curlHandler->setAuthName($this->auth_username);
$this->curlHandler->setAuthPass($this->auth_password);
}
/**
* @param $name
* @param $hash
* @param string $signedHash
* @param string $pubKey
* @param string $signature
* @param string $identityURL
* @param bool $public
* @param bool $notifyByEmail
* @param array $tags
* @param array $metadata
* @param string $callbackURL
* @return mixed
*/
public function createAnchor($name, $hash, $signedHash = "", $pubKey = "", $signature = "", $identityURL = "", $public = true, $notifyByEmail = false, $tags = array(), $metadata = array(), $callbackURL = "") {
$anchor_data = array("name" => $name, "hash" => $hash, "public" => $public, "notifyByEmail" => $notifyByEmail);
if (!empty($signedHash)) {
$anchor_data["signedHash"] = $signedHash;
}
if (!empty($pubKey)) {
$anchor_data["pubKey"] = $pubKey;
}
if (!empty($signature)) {
$anchor_data["signature"] = $signature;
}
if (!empty($identityURL)) {
$anchor_data["identityURL"] = $identityURL;
}
if (!empty($tags)) {
$anchor_data["tags"] = $tags;
}
if (!empty($metadata)) {
$anchor_data["metadata"] = $metadata;
}
if (!empty($callbackURL)) {
$anchor_data["callbackURL"] = $callbackURL;
}
$this->curlHandler->useAuth(true);
$this->curlHandler->makeHTTPHeader("application/json", "application/json");
$this->curlHandler->setIncludeHeader(true);
/*echo "<pre>";
print_r(json_encode($anchor_data));
echo "</pre>";*/
$this->curlHandler->post("/anchor", json_encode($anchor_data));
$result = $this->curlHandler->getResponse();
return $result;
}
public function getAnchor($anchor_id) {
$this->curlHandler->useAuth(true);
$this->curlHandler->makeHTTPHeader("application/json", "application/json");
$this->curlHandler->setIncludeHeader(true);
$this->curlHandler->get("/anchor/" . $anchor_id, null);
$result = $this->curlHandler->getResponse();
return $result;
}
public function updateAnchor($anchor_id, $name, $hash, $signedHash = "", $pubKey = "", $signature = "",
$identityURL = "", $public = true, $notifyByEmail = false, $tags = array(),
$metadata = array(), $callbackURL = "") {
$anchor_data = array("name" => $name, "hash" => $hash, "public" => $public, "notifyByEmail" => $notifyByEmail);
if (!empty($signedHash)) {
$anchor_data["signedHash"] = $signedHash;
}
if (!empty($pubKey)) {
$anchor_data["pubKey"] = $pubKey;
}
if (!empty($signature)) {
$anchor_data["signature"] = $signature;
}
if (!empty($identityURL)) {
$anchor_data["identityURL"] = $identityURL;
}
if (!empty($tags)) {
$anchor_data["tags"] = $tags;
}
if (!empty($metadata)) {
$anchor_data["metadata"] = $metadata;
}
if (!empty($callbackURL)) {
$anchor_data["callbackURL"] = $callbackURL;
}
$this->curlHandler->useAuth(true);
$this->curlHandler->makeHTTPHeader("application/json", "application/json");
$this->curlHandler->setIncludeHeader(true);
/*echo "<pre>";
print_r(json_encode($anchor_data));
echo "</pre>";*/
$this->curlHandler->put("/anchor/" . $anchor_id, json_encode($anchor_data));
$result = $this->curlHandler->getResponse();
print($this->curlHandler->getCurlError());
return $result;
}
public function deleteAnchor($anchor_id) {
$this->curlHandler->useAuth(true);
$this->curlHandler->makeHTTPHeader("application/json", "application/json");
$this->curlHandler->setIncludeHeader(true);
$this->curlHandler->delete("/anchor/" . $anchor_id, null);
$result = $this->curlHandler->getResponse();
return $result;
}
}
UNIT TEST:
require_once ("classes/classWoleetManager.php");
class WoleetManagerTest extends PHPUnit_Framework_TestCase {
/** @var WoleetManager $woleetManager */
protected $woleetManager;
public function setUp(){
$this->woleetManager = new WoleetManager();
}
public function tearDown(){
unset($this->woleetManager);
}
public function testCreateAnchor() {
$result= $this->woleetManager->createAnchor("test callback 2", "b8f73260adff5c418c6e4b0127dddbe2e783eb4ade074c3423f5dd216b9c55e2","", "", "", "", true,
false, array("test"), array("batch_id"=>1, "client_id"=>2, "report_id"=>3, "file_id"=>3), "http://blockchainhuissieray.com/woleetCallBackHandler.php");
print($result);
}
public function testGetAnchor(){
$result= $this->woleetManager->getAnchor("cacdb893-53e1-4af2-a761-7e88f7d3481e");
print_r($result);
}
public function testDeleteAnchor(){
$result= $this->woleetManager->deleteAnchor("cacdb893-53e1-4af2-a761-7e88f7d3481e");
print_r($result);
}
public function testUpdateAnchor(){
$result= $this->woleetManager->updateAnchor("cacdb893-53e1-4af2-a761-7e88f7d3481e", "test updated new", "b8f73260adff5c418c6e4b0127dddbe2e783eb4ade074c3423f5dd216b9c55e2","", "", "", "", true,
false, array("test"), array("batch_id"=>1, "client_id"=>2, "report_id"=>3, "file_id"=>3), "http://blockchainhuissieray.com/woleetCallBackHandler.php");
print_r($result);
}
}