Merge pull request #13374 from cividesk/dev-627
[civicrm-core.git] / api / v3 / examples / Contact / Create.php
CommitLineData
6a488035 1<?php
50fb255d 2/**
a828d7b8 3 * Test Generated example demonstrating the Contact.create API.
50fb255d 4 *
5c49fee0 5 * This demonstrates setting a custom field through the API.
50fb255d 6 *
7 * @return array
8 * API result array
9 */
10function contact_create_example() {
cf8f0fff 11 $params = [
50fb255d 12 'first_name' => 'abc1',
13 'contact_type' => 'Individual',
14 'last_name' => 'xyz1',
15 'custom_1' => 'custom string',
cf8f0fff 16 ];
6a488035 17
50fb255d 18 try{
a828d7b8 19 $result = civicrm_api3('Contact', 'create', $params);
50fb255d 20 }
21 catch (CiviCRM_API3_Exception $e) {
22 // Handle error here.
23 $errorMessage = $e->getMessage();
24 $errorCode = $e->getErrorCode();
25 $errorData = $e->getExtraParams();
cf8f0fff 26 return [
5be22f39 27 'is_error' => 1,
28 'error_message' => $errorMessage,
50fb255d 29 'error_code' => $errorCode,
30 'error_data' => $errorData,
cf8f0fff 31 ];
50fb255d 32 }
33
34 return $result;
6a488035
TO
35}
36
fb32de45 37/**
50fb255d 38 * Function returns array of result expected from previous function.
39 *
40 * @return array
41 * API result array
6a488035 42 */
50fb255d 43function contact_create_expectedresult() {
6a488035 44
cf8f0fff 45 $expectedResult = [
50fb255d 46 'is_error' => 0,
47 'version' => 3,
48 'count' => 1,
1d8ee3d6 49 'id' => 3,
cf8f0fff
CW
50 'values' => [
51 '3' => [
1d8ee3d6 52 'id' => '3',
50fb255d 53 'contact_type' => 'Individual',
54 'contact_sub_type' => '',
55 'do_not_email' => 0,
56 'do_not_phone' => 0,
57 'do_not_mail' => 0,
58 'do_not_sms' => 0,
59 'do_not_trade' => 0,
60 'is_opt_out' => 0,
61 'legal_identifier' => '',
62 'external_identifier' => '',
63 'sort_name' => 'xyz1, abc1',
64 'display_name' => 'abc1 xyz1',
65 'nick_name' => '',
66 'legal_name' => '',
67 'image_URL' => '',
68 'preferred_communication_method' => '',
69 'preferred_language' => 'en_US',
70 'preferred_mail_format' => 'Both',
71 'hash' => '67eac7789eaee00',
72 'api_key' => '',
73 'first_name' => 'abc1',
74 'middle_name' => '',
75 'last_name' => 'xyz1',
76 'prefix_id' => '',
77 'suffix_id' => '',
78 'formal_title' => '',
79 'communication_style_id' => '',
80 'email_greeting_id' => '1',
81 'email_greeting_custom' => '',
82 'email_greeting_display' => '',
83 'postal_greeting_id' => '1',
84 'postal_greeting_custom' => '',
85 'postal_greeting_display' => '',
86 'addressee_id' => '1',
87 'addressee_custom' => '',
88 'addressee_display' => '',
89 'job_title' => '',
90 'gender_id' => '',
91 'birth_date' => '',
92 'is_deceased' => 0,
93 'deceased_date' => '',
94 'household_name' => '',
95 'primary_contact_id' => '',
96 'organization_name' => '',
97 'sic_code' => '',
98 'user_unique_id' => '',
99 'created_date' => '2013-07-28 08:49:19',
100 'modified_date' => '2012-11-14 16:02:35',
cf8f0fff
CW
101 ],
102 ],
103 ];
6a488035
TO
104
105 return $expectedResult;
106}
107
a828d7b8 108/*
50fb255d 109* This example has been generated from the API test suite.
a828d7b8
CW
110* The test that created it is called "testCreateWithCustom"
111* and can be found at:
69d79249 112* https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/ContactTest.php
1c88e578 113*
114* You can see the outcome of the API tests at
69d79249 115* https://test.civicrm.org/job/CiviCRM-master-git/
fb32de45 116*
117* To Learn about the API read
69d79249 118* http://wiki.civicrm.org/confluence/display/CRMDOC/Using+the+API
fb32de45 119*
69d79249 120* Browse the api on your own site with the api explorer
41d4d31f 121* http://MYSITE.ORG/path/to/civicrm/api
fb32de45 122*
6a488035
TO
123* Read more about testing here
124* http://wiki.civicrm.org/confluence/display/CRM/Testing
fb32de45 125*
126* API Standards documentation:
127* http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards
b259a4ab 128*/