This topic contains 1 reply, has 1 voice, and was last updated by zhex900 6 years, 8 months ago.
- Topic
Hi,
I am trying to learn how to use the OROCRM v 2.3 API. I can retrieve information like contacts and users. This code for creating a new contact gives this error. I have added some extra fields.
1234string(1158) "{"code":400,"message":"Validation Failed","errors":{"errors":["At least one of the fieldsFirst name, Last name, Emails or Phones must be defined."],"children":{"owner":{},"user":{},"country_of_birth":{},"date_of_baptism":{},"student_id":{},"year_of_birth":{},"int_student":{},"out_of_town":{},"christian":{},"believed_thr_us":{},"baptised_by_us":{},"first_contact_date":{},"date_believed":{},"duration":{},"year_of_commencement":{},"semester_contacted":{},"ethnicity":{},"marital_status":{},"contact_source":{},"contact_status":{},"degrees":{},"level_of_study":{},"month_of_commencement":{},"institutions":{},"further_activities":{},"bible_study":{},"homes":{},"sunday_worship":{},"mhl":{},"christian_courses":{},"auto_allocate":{},"church_kid":{},"namePrefix":{},"firstName":{},"middleName":{},"lastName":{},"nameSuffix":{},"gender":{},"birthday":{},"description":{},"jobTitle":{},"fax":{},"skype":{},"twitter":{},"facebook":{},"googlePlus":{},"linkedIn":{},"picture":{"children":{"file":{}}},"source":{},"assignedTo":{},"reportsTo":{},"method":{},"addresses":{},"emails":{},"phones":{},"groups":{},"appendAccounts":{},"removeAccounts":{},"createdAt":{}}}}"123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109define("URL", "http://orocampus.tk/api/rest/latest/");define("CONTACT_SEARCH", "");define("CONTACT_ADD", "contacts");define("CONTACT_TEST", "contacts/1");define("APIKEY", "0de5698762d0a7eaaasomethin_mykey00000e");define("LOGIN","admin");//$api = new ApiRestHelper(URL,LOGIN,APIKEY);//$result = $api->curl_req(CONTACT_TEST);//var_dump($result);$new_contact =['data' => ['type' => 'contacts','attributes' => ['firstName' => 'Apple','lastName' => 'Pie','primaryPhone' => '0433169154','primaryEmail' => 'applepie@gmail.com','birthday' => '1995-05-25'],'relationships' => ['owner' => ['data' => ['type' => 'users','id'=> '1']]],'organization' => ['data' => ['type' => 'organizations','id'=> '1']]]];$result = $api->curl_req(CONTACT_ADD,null,$new_contact);var_dump($result);/** @return bool* Check if the orocampus.tk API is alive.*/function isAPIUp(){/** @var ApiRestHelper $api */$api = new ApiRestHelper(URL,LOGIN,APIKEY);return !empty($api->curl_req(CONTACT_TEST));}class ApiRestHelper{protected $_url;protected $_username;protected $_apiKey;public function __construct($url, $username, $apiUserKey) {$this->_url = $url;$this->_username = $username;$this->_apiKey = $apiUserKey;}private function getHeader() {$nonce = uniqid();$created = date('c');$digest = base64_encode(sha1(base64_decode($nonce) . $created . $this->_apiKey, true));$wsseHeader = "Authorization: WSSE profile=\"UsernameToken\"\n";$wsseHeader .= sprintf('X-WSSE: UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"', $this->_username, $digest, $nonce, $created);return $wsseHeader;}public function curl_req($path, $verb=NULL, $data=array()) {$wsseHeader[] = "Accept: application/vnd.api+json";$wsseHeader[] = $this->getHeader();$options = array(CURLOPT_URL => $this->_url . $path,CURLOPT_HTTPHEADER => $wsseHeader,CURLOPT_RETURNTRANSFER => true,CURLOPT_HEADER => false);if( !empty($data) ) {$options += array(CURLOPT_POSTFIELDS => json_encode($data),CURLOPT_SAFE_UPLOAD => true);}if( isset($verb) ) {$options += array(CURLOPT_CUSTOMREQUEST => $verb);}$ch = curl_init();curl_setopt_array($ch, $options);$result = curl_exec($ch);if(false === $result ) {echo curl_error($ch);}curl_close($ch);return $result;}}
The forum ‘OroCRM – How do I? Questions’ is closed to new topics and replies.