Monthly Archives: September 2016

OroCRM Web API Usage

In OroCRM 1.10 a new REST/JSON API was introduced. This post is about how to use it.

Basics:
1. You need to create an API key for the user you are going to use for API based access. Instructions are at OroCRM’s How to use WSSE authentication.
2. The full documentation about the API is auto generated. Go to [app_url]/api/doc/rest_json_api .
3. API Sandbox test is available in the page. This is crucial to understanding and using the API from your own apps.
4. Login as the API user so you can use the API Sandbox testing.
5. Click on “Sandbox” to try the API.
6. Enter the parameters and click “Try It”
7. The response returned gives the format for JSON you can use in your API.

Web API for OroCRM

PHP Code for API access.

  1. Generate Auth Header

 

 public function generateWsseAuthHeader() 
 {
 
 $userName = $this->userName;
 $userSecret = $this->api_token==='0'?$this->accountpassword:$this->api_token ;
 $nonce = null;
 
 if (null === $nonce) 
 {
   $nonce = substr($this->nonce_prefix . uniqid(), 0, 16);
 }
 
 $created = date('c');
 $digest = base64_encode(sha1(base64_decode($nonce) . $created . $userSecret, true));
 $wsseHeader = [
  'content-type' => 'application/vnd.api+json',
  'Authorization' => 'WSSE profile="UsernameToken"',
  'X-WSSE' => sprintf(
  'UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"',
  $userName,
  $digest,
  $nonce,
  $created
   )
  ];
 
 return $wsseHeader;
 }

2. Example of Web API (Create a Call Log)


 protected function OroDataArray($type, $id)
    {
        return  (['data' => ['type' => $type, 'id' => $id ] ]);
    }
    
    public function LogPhoneCall($from, 
                                 $to, 
                                 $direction,
                                 $duration_sec, 
                                 $subject, 
                                 $notes,
                                 $yr, $month, $day, $hr24, $minute, $sec)
    {
        
        $url = $this->crm_base_url . "api/calls";
        $client = new \GuzzleHttp\Client();
        $WsseHeaders = $this->generateWsseAuthHeader();
       
        $request = [
            "data" => [
                "type"         =>  'calls',
                "attributes"   => 
                    [
                        "subject"      => $subject,
                        "duration"     => $duration_sec,
                        "callDateTime" => date('c'),
                        "phoneNumber"  => $from,
                        "notes"        => 'Incoming Call'
                    ], //end attrib
                "relationships" => [
                   "owner"        => 
                        $this->OroDataArray('users',
                                            '1' ),
                   "callStatus"   => 
                        $this->OroDataArray('callstatuses',
                                            'completed' ),
                    "direction"    => 
                        $this->OroDataArray('calldirections',
                                             $direction ),
                    "organization" => 
                        $this->OroDataArray('organizations',
                                            '1' )
               ], //end relationships
            ] //end data
        ]; //end request
        
        $res =$client->request('POST', $url, [  
                'headers'  => $WsseHeaders,
                'verify'   => false, 
                'body'     => json_encode($request),
                ] );
        $responsecode = $res->getStatusCode();
        if ($responsecode < 300)
        {
            $contents = (string) $res->getBody(); 
            $d = json_decode ($contents, true);
            return  $d['data']['id'];
        }
        return "0";
   } //end LogPhoneCall

References:
http://www.sylvainraye.com/2014/03/23/using-the-rest-api-of-oroplatform-orocrm-akeneo/

Creating Records using Rest Webservice


https://github.com/orocrm/OroCRMCallBundle/blob/master/Resources/doc/reference/api.md [Note its not exactly accurate – always use auto generated docs]

OroCRM Install Notes

Short post about OroCRM 1.10 on CentOS 7.2.  The original install instructions are good. If you have the OroCRM behind a reverse proxy which is not HTTPS, then you need to configure trusted proxy otherwise, there will be errors.

In: app/config/config.yml

Set:

    trusted_proxies:  [192.0.0.1, 10.0.0.0/8,  cloud_flare_ip  ]

Also, change sending address in System -> Email Configuration if it has taken localdomain or something not resolvable from outside. Otherwise outgoing mails may be blocked.