|
| 1 | +<?php |
| 2 | + |
| 3 | +# Enter your domain name , agile email and agile api key |
| 4 | + |
| 5 | +define("AGILE_DOMAIN", "your_agile_domain"); # Example : define("domain","jim"); |
| 6 | +define("AGILE_USER_EMAIL", "your_agile_user_email"); |
| 7 | +define("AGILE_REST_API_KEY", "your_agile_api_key"); // Example : http://snag.gy/AEq23.jpg |
| 8 | + |
| 9 | +function curl_wrap($entity, $data, $method) |
| 10 | +{ |
| 11 | + $agile_url = "https://" . AGILE_DOMAIN . ".agilecrm.com/dev/api/" . $entity; |
| 12 | + $agile_php_url = "https://" . AGILE_DOMAIN . ".agilecrm.com/core/php/api/" . $entity . "?id=" . AGILE_REST_API_KEY; |
| 13 | + |
| 14 | + $ch = curl_init(); |
| 15 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
| 16 | + curl_setopt($ch, CURLOPT_MAXREDIRS, 10); |
| 17 | + curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, true); |
| 18 | + |
| 19 | + switch ($method) { |
| 20 | + case "POST": |
| 21 | + $url = ($entity == "tags" ? $agile_php_url : $agile_url); |
| 22 | + curl_setopt($ch, CURLOPT_URL, $url); |
| 23 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); |
| 24 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
| 25 | + break; |
| 26 | + case "GET": |
| 27 | + $url = ($entity == "tags" ? $agile_php_url . '&email=' . $data->{'email'} : $agile_url); |
| 28 | + curl_setopt($ch, CURLOPT_URL, $url); |
| 29 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); |
| 30 | + break; |
| 31 | + case "PUT": |
| 32 | + $url = ($entity == "tags" ? $agile_php_url : $agile_url); |
| 33 | + curl_setopt($ch, CURLOPT_URL, $url); |
| 34 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); |
| 35 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
| 36 | + break; |
| 37 | + case "DELETE": |
| 38 | + $url = ($entity == "tags" ? $agile_php_url : $agile_url); |
| 39 | + curl_setopt($ch, CURLOPT_URL, $url); |
| 40 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); |
| 41 | + break; |
| 42 | + default: |
| 43 | + break; |
| 44 | + } |
| 45 | + |
| 46 | + curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
| 47 | + 'Content-type : application/json;','Accept : application/json' |
| 48 | + )); |
| 49 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 50 | + curl_setopt($ch, CURLOPT_USERPWD, AGILE_USER_EMAIL . ':' . AGILE_REST_API_KEY); |
| 51 | + curl_setopt($ch, CURLOPT_TIMEOUT, 120); |
| 52 | +curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
| 53 | + $output = curl_exec($ch); |
| 54 | + curl_close($ch); |
| 55 | + return $output; |
| 56 | +} |
| 57 | + |
| 58 | +echo "<html><head></head><body>"; |
| 59 | +echo "<h2>Reference taken from : https://github.com/agilecrm/php-api</h2>"; |
| 60 | + |
| 61 | +echo "<ul>"; |
| 62 | + |
| 63 | +/*================================================To create a contact ================================================*/ |
| 64 | +$address = array( |
| 65 | + "address"=>"Avenida Álvares Cabral 1777", |
| 66 | + "city"=>"Belo Horizonte", |
| 67 | + "state"=>"Minas Gerais", |
| 68 | + "country"=>"Brazil" |
| 69 | +); |
| 70 | + |
| 71 | +$contact_email = "[email protected]"; |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +$new_contact_json = array( |
| 76 | + "lead_score"=>"24", |
| 77 | + "star_value"=>"4", |
| 78 | + "tags"=>array("test1","test2"), |
| 79 | + "properties"=>array( |
| 80 | + array( |
| 81 | + "name"=>"first_name", |
| 82 | + "value"=>"Ronaldo", |
| 83 | + "type"=>"SYSTEM" |
| 84 | + ), |
| 85 | + array( |
| 86 | + "name"=>"last_name", |
| 87 | + "value"=>"de Lima", |
| 88 | + "type"=>"SYSTEM" |
| 89 | + ), |
| 90 | + array( |
| 91 | + "name"=>"email", |
| 92 | + "value"=>$contact_email, |
| 93 | + "type"=>"SYSTEM" |
| 94 | + ), |
| 95 | + array( |
| 96 | + "name"=>"title", |
| 97 | + "value"=>"the phenomenon", |
| 98 | + "type"=>"SYSTEM" |
| 99 | + ), |
| 100 | + array( |
| 101 | + "name"=>"image", |
| 102 | + "value"=>"http://www.soccerticketsonline.com/wp-content/uploads/ronaldo9.jpg", //This image value may be url or path of image |
| 103 | + "type"=>"SYSTEM" |
| 104 | + ), |
| 105 | + array( |
| 106 | + "name"=>"company", |
| 107 | + "value"=>"ibm", |
| 108 | + "type"=>"SYSTEM" |
| 109 | + ), |
| 110 | + array( |
| 111 | + "name"=>"address", |
| 112 | + "value"=>json_encode($address), |
| 113 | + "type"=>"SYSTEM" |
| 114 | + ), |
| 115 | + array( |
| 116 | + "name"=>"phone", |
| 117 | + "value"=>"+1-541-754-3030", |
| 118 | + "type"=>"SYSTEM" |
| 119 | + ), |
| 120 | + array( |
| 121 | + "name"=>"website", |
| 122 | + "value"=>"http://www.google.com", |
| 123 | + "type"=>"SYSTEM" |
| 124 | + ), |
| 125 | + array( |
| 126 | + "name"=>"experience in field", //This is custom field which you should first define in custom field region. |
| 127 | + //Example - created custom field : http://snag.gy/kLeQ0.jpg |
| 128 | + "value"=>"5", |
| 129 | + "type"=>"CUSTOM" |
| 130 | + ), |
| 131 | + array( |
| 132 | + "name"=>"Date Of Joining", |
| 133 | + "value"=>"1438951923", // This is epoch time in seconds. |
| 134 | + "type"=>"CUSTOM" |
| 135 | + ) |
| 136 | + |
| 137 | + ) |
| 138 | +); |
| 139 | +$new_contact_json = json_encode($new_contact_json); |
| 140 | + |
| 141 | + |
| 142 | +echo "<li>contact created with following data</li><br>"; |
| 143 | +echo "<li>" . $new_contact_json . "</li><br>"; |
| 144 | +$result = curl_wrap("contacts", $new_contact_json, "POST"); |
| 145 | +echo "<li>created contact data is ...</li><br>"; |
| 146 | +echo "<li>" . $result . "</li>"; |
| 147 | +echo "<br><hr><br>"; |
| 148 | + |
| 149 | +/*================================================= create contact end ================================================*/ |
| 150 | + |
| 151 | +/*================================================= update contact ================================================*/ |
| 152 | + |
| 153 | +$address1 = array( |
| 154 | + "address"=>"Avenida Álvares Cabral 1777", |
| 155 | + "city"=>"Belo Horizonte", |
| 156 | + "state"=>"Minas Gerais", |
| 157 | + "country"=>"Brazil" |
| 158 | +); |
| 159 | + |
| 160 | +$contact_email1 = "[email protected]"; |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | +$new_contact_json1 = array( |
| 165 | + "id"=> "5706163895140352", // This contact id is related to [email protected]. |
| 166 | + //And it is mandatory. Example contact id :http://snag.gy/M6S3A.jpg |
| 167 | + //You can get contact id of email which is described below. |
| 168 | + "lead_score"=>"24", |
| 169 | + "star_value"=>"4", |
| 170 | + "tags"=>array("test1","test2"), |
| 171 | + "properties"=>array( |
| 172 | + array( |
| 173 | + "name"=>"first_name", |
| 174 | + "value"=>"ArnoldUpdated", |
| 175 | + "type"=>"SYSTEM" |
| 176 | + ), |
| 177 | + array( |
| 178 | + "name"=>"last_name", |
| 179 | + "value"=>"hello", |
| 180 | + "type"=>"SYSTEM" |
| 181 | + ), |
| 182 | + array( |
| 183 | + "name"=>"email", |
| 184 | + "value"=>$contact_email1, |
| 185 | + "type"=>"SYSTEM" |
| 186 | + ), |
| 187 | + array( |
| 188 | + "name"=>"title", |
| 189 | + "value"=>"the phenomenon", |
| 190 | + "type"=>"SYSTEM" |
| 191 | + ), |
| 192 | + array( |
| 193 | + "name"=>"company", |
| 194 | + "value"=>"ibm", |
| 195 | + "type"=>"SYSTEM" |
| 196 | + ), |
| 197 | + array( |
| 198 | + "name"=>"address", |
| 199 | + "value"=>json_encode($address1), |
| 200 | + "type"=>"SYSTEM" |
| 201 | + ), |
| 202 | + array( |
| 203 | + "name"=>"phone", |
| 204 | + "value"=>"+1-541-754-3030", |
| 205 | + "type"=>"SYSTEM" |
| 206 | + ), |
| 207 | + array( |
| 208 | + "name"=>"website", |
| 209 | + "value"=>"http://www.google.com", |
| 210 | + "type"=>"SYSTEM" |
| 211 | + ), |
| 212 | + array( |
| 213 | + "name"=>"experience in field", |
| 214 | + "value"=>"5", |
| 215 | + "type"=>"CUSTOM" |
| 216 | + ), |
| 217 | + array( |
| 218 | + "name"=>"Date Of Joining", |
| 219 | + "value"=>"1438951923", // This is epoch time in seconds. |
| 220 | + "type"=>"CUSTOM" |
| 221 | + ) |
| 222 | + ) |
| 223 | +); |
| 224 | +$new_contact_json1 = json_encode($new_contact_json1); |
| 225 | + |
| 226 | + |
| 227 | +echo "<li>contact updated with following data</li><br>"; |
| 228 | +echo "<li>" . $new_contact_json1 . "</li><br>"; |
| 229 | +$result = curl_wrap("contacts", $new_contact_json1, "PUT"); |
| 230 | +echo "<li>updated contact data is ...</li><br>"; |
| 231 | +echo "<li>" . $result . "</li>"; |
| 232 | +echo "<br><hr><br>"; |
| 233 | + |
| 234 | +/*===========================================update contact end================================================*/ |
| 235 | + |
| 236 | + |
| 237 | +/* ------------------------------------get contact by email and get contact id also ---------------------------- */ |
| 238 | + |
| 239 | +echo "<li>get contact with email id, and find also get conatct id of this contact</li><br>"; |
| 240 | +echo "<br>"; |
| 241 | +$result = curl_wrap( "contacts/search/email/[email protected]", null, "GET"); |
| 242 | +echo "<li>contact data received is ...</li><br>"; |
| 243 | +echo "<li>" . $result . "</li><br>"; |
| 244 | + |
| 245 | +echo "<li>contact id of contact received is ...</li><br>"; |
| 246 | + |
| 247 | +$result = json_decode($result, true); |
| 248 | +$contact_id = $result['id']; |
| 249 | +$contact_id1 = number_format($contact_id,0,'',''); |
| 250 | +echo "<li>" . $contact_id1 . "</li>"; |
| 251 | +echo "<br><hr><br>"; |
| 252 | +/* ------------------------------------get contact by email END----------------------------------------- */ |
| 253 | + |
| 254 | +/* ------------------------------------get contact by contact_id ----------------------------------------- */ |
| 255 | +echo "<li>get contact by conatct id</li><br>"; |
| 256 | +echo "<br>"; |
| 257 | +$result = curl_wrap("contacts/5706163895140352", null, "GET"); // More info :https://github.com/agilecrm/php-api#by-id |
| 258 | +echo "<li>contact data received is ...</li><br>"; |
| 259 | +echo "<li>" . $result . "</li>"; |
| 260 | + |
| 261 | +echo "<br><hr><br>"; |
| 262 | +/* ------------------------------------get contact by contact_id END----------------------------------------- */ |
| 263 | + |
| 264 | +/* ------------------------------------delete contact by contact_id ----------------------------------------- */ |
| 265 | +echo "<li>get contact with email id and find also conatct id</li><br>"; |
| 266 | +echo "<br>"; |
| 267 | +$result = curl_wrap("contacts/5632908932939776", null, "DELETE"); |
| 268 | +echo "<li>deleted response received is ...</li><br>"; |
| 269 | +echo "<li>" . $result . "</li>"; |
| 270 | + |
| 271 | +echo "<br><hr><br>"; |
| 272 | +/* ------------------------------------delete contact by contact_id END----------------------------------------- */ |
| 273 | + |
| 274 | + |
| 275 | +/*=================================================== create deal=======================================*/ |
| 276 | + |
| 277 | + $opportunity_json = array( |
| 278 | + "name"=>"test deal1", |
| 279 | + "description"=>"this is a test deal", |
| 280 | + "expected_value"=>1000, |
| 281 | + "milestone"=>"New", |
| 282 | + "custom_data"=>array( //This is custom field which you should first define in custom field region. |
| 283 | + //Example :http://snag.gy/OOuj8.jpg |
| 284 | + array( |
| 285 | + "name"=>"dataone", |
| 286 | + "value"=>"xyz" |
| 287 | + ), |
| 288 | + array( |
| 289 | + "name"=>"datatwo", |
| 290 | + "value"=>"abc" |
| 291 | + ) |
| 292 | + ), |
| 293 | + "probability"=>50, |
| 294 | + "close_date"=>1438948949, |
| 295 | + "contact_ids"=>array("5706163895140352") // Contact ID to which deal going to added |
| 296 | +); |
| 297 | +$opportunity_json = json_encode($opportunity_json); |
| 298 | +echo "<li>create deal with below data</li><br>"; |
| 299 | +echo "<li>" . $opportunity_json . "</li><br>"; |
| 300 | +$result = curl_wrap("opportunity", $opportunity_json, "POST"); |
| 301 | +echo "<li>created deal data is ...</li><br>"; |
| 302 | +echo "<li>" . $result . "</li>"; |
| 303 | +echo "<br><hr><br>"; |
| 304 | + |
| 305 | +/*===================================================== create deal end================================================*/ |
| 306 | + |
| 307 | +/*===================================================== update deal================================================*/ |
| 308 | + |
| 309 | + $opportunity_json = array( |
| 310 | + "id"=>"5675214360805376", // Deal ID. Example : http://snag.gy/SqMqF.jpg |
| 311 | + "name"=>"test deal1 updated", |
| 312 | + "description"=>"this is a test deal", |
| 313 | + "expected_value"=>1000, |
| 314 | + "milestone"=>"Won", // Changed milestone type from New to Won for existing deal. |
| 315 | + "custom_data"=>array( |
| 316 | + array( |
| 317 | + "name"=>"dataone", |
| 318 | + "value"=>"xyz updated" |
| 319 | + ), |
| 320 | + array( |
| 321 | + "name"=>"datatwo", |
| 322 | + "value"=>"abc updated" |
| 323 | + ) |
| 324 | + ), |
| 325 | + "probability"=>50, |
| 326 | + "close_date"=>1438949981, |
| 327 | + "contact_ids"=>array("5706163895140352") // Contact ID to which deal going to added. |
| 328 | +); |
| 329 | +$opportunity_json = json_encode($opportunity_json); |
| 330 | +echo "<li>update deal with below data</li><br>"; |
| 331 | +echo "<li>" . $opportunity_json . "</li><br>"; |
| 332 | +$result = curl_wrap("opportunity", $opportunity_json, "POST"); |
| 333 | +echo "<li>updated deal data is ...</li><br>"; |
| 334 | +echo "<li>" . $result . "</li>"; |
| 335 | +echo "<br><hr><br>"; |
| 336 | + |
| 337 | +/*================================================= update deal end================================================*/ |
| 338 | +echo "</ul>"; |
| 339 | +echo "</body></html>"; |
| 340 | +?> |
0 commit comments