Forums › Forums › OroCRM › OroCRM – How do I? Questions › How to use the new Web API – Sample Code
This topic contains 8 replies, has 5 voices, and was last updated by jithin 4 years, 6 months ago.
Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.
- CreatorTopic
- September 8, 2016 at 4:05 am #29056
In reference to the new web API: https://oroinc.com/orocrm/blog/product-updates/new-oro-web-api-now-available
Can you please provide some sample code? for example:
1. How to enable listing all leads or contacts
2. How to setup authentication
3. How to actually CURL to get the listI have checked: https://github.com/orocrm/platform/blob/master/src/Oro/Bundle/ApiBundle/Resources/doc/index.md
but I’m not sure how to apply it to OroCRM. An example would make it very easy.Thanks much.
- CreatorTopic
- AuthorReplies
- September 8, 2016 at 7:28 am #29057
Hi!
I have described how to create extend entity there. Similarly you can create other entities.
After you generated api key and created wsse header, this command returns all contacts:
Shell1curl -i -H "Content-type:application/vnd.api+json" -H "Accept: application/json" -H 'Authorization: WSSE profile="UsernameToken"' -H 'X-WSSE: UsernameToken Username="admin", PasswordDigest="QR0OxwIGN/rivKNryPv6KYRpN4I=", Nonce="ZDNkMmYzY2FlZjIyYmM1Mw==", Created="2016-09-08T14:15:29+00:00"' http://orocrm-application.local/app_dev.php/api/contactsSeptember 11, 2016 at 1:11 pm #29058Thanks.This was very helpful.
September 18, 2016 at 12:24 pm #29059Hello Mike,
Can you also give an example of a PATCH example? i’m trying a simple lead e-mail patch:PHP1234567891011//patch leademail$apicall = "leademails/1"; // update e-mail with id 1.$dataUpdate = array ( 'email' => 'test@doens.nl');$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url.$apicall);curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //voorkomt output als echocurl_setopt($ch, CURLOPT_HTTPHEADER, wsseheader($userName, $userPassword));curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($dataUpdate));$data = curl_exec($ch);$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);wsse & api is working, but i get:
[{“title”:”extra fields constraint”,”detail”:”This form should not contain extra fields: \”{\”email\”:\”test@doens_nl\”}\””}]
September 22, 2016 at 9:04 am #29060Hi,
I have tried something like this and it works. Probably $dataUpdate should contains json presented below.
testing.sh:
Shell12345#!/bin/shcurl -i --data @testing.json -X "PATCH" -H "Content-type:application/vnd.api+json" -H "Accept: application/json" -H 'Authorization: WSSE p-H 'X-WSSE: UsernameToken Username="admin", PasswordDigest="FGCYI3vdYK63dmCkw49POmQVBaw=", Nonce="NjdkYTdmZjkyM2UwMThiZg==", Created="2016http://orocrm-application.local/api/leademails/2testing.json:
JavaScript12345678910{"data":{"type": "leademails","id": "2","attributes": {"email": "custom3@custom.com"}}}September 26, 2016 at 8:20 am #29061Hello Mike,
i got it working, it was the $data array + wrong content-type header, it has to be /vnd.api+json an nothing differentfull example including wsse header:
PHP1234567891011121314151617181920212223242526272829303132333435363738394041424344<?php$userName = 'oro';$userPassword = 'api key of user here and not the password!';$url = "https://crm.yourdomain.com/api/";$apicall = "leademails/1"; // update e-mail with id 1, change for different id// php array with data to update record 1$dataUpdate = array ( 'data' => array ( 'type' => 'leademails' ,'id' => '1' , 'attributes' => array ( 'email' => 'test2@doens.nl' ) ));$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url.$apicall);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); //LET OP!! PATCH voor updates!!curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($dataUpdate));curl_setopt($ch, CURLOPT_HTTPHEADER, wsseheader($userName, $userPassword,$dataUpdate, $url, $apicall));$data = curl_exec($ch);$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);echo $httpcode;// function to generate wsseheaderfunction wsseheader($userName, $userPassword) {$nonce = uniqid();$created = date('c');$digest = base64_encode(sha1(base64_decode($nonce) . $created . $userPassword, true));$wsseHeader = array();//$wsseHeader[] = "Accept: application/json";$wsseHeader[] = "Content-Type: application/vnd.api+json";$wsseHeader[] = "Authorization: WSSE profile=\"UsernameToken\"";$code = sprintf('X-WSSE: UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"',$userName,$digest,$nonce,$created);$wsseHeader[] = $code;return $wsseHeader;}?>December 7, 2016 at 7:52 am #29062If it could be usefull for anyone :
PHP12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061class Request {protected $_url;protected $_username;protected $_apiKey;public function __construct($url, $username, $apiUserKey) {$this->_url = $url;$this->_username = $username;$this->_apiKey = $apiUserKey;}public 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 => $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;}}September 30, 2019 at 5:27 am #40590Can someone send a sample PHP code to list all contacts in OROcrm?
October 1, 2019 at 3:11 am #40601Hi Andrey, Thanks for the reply. I’ve been trying to list accounts and contacts from OROCRM. Its working fine from OROCRMurl/api/doc/rest_json_api. But when i try the same request with CURL command in the command line am getting a 401 Unauthorized error. I am using PHP curl in my scripts. I get same 401 error from PHP script as well. I made sure headers are generated for each requests. Do you have any idea why i am getting “401 Unauthorized”?
- AuthorReplies
The forum ‘OroCRM – How do I? Questions’ is closed to new topics and replies.