AnsweredAssumed Answered

Post JSON data with cURL using Module Loader

Question asked by Manuel Rubio on Feb 25, 2016
Latest reply on Feb 26, 2016 by Manuel Rubio

Hi. I created an installable package for a logic hook that makes a post request to an URL using cURL and JSON. But I have a problem. When I access the module and create the record the first one it always get duplicated, and subsequently the record created in the URL gets duplicated too. What can be wrong? or how can I fix it?

 

Here is the manifest.php code:

 

<?php

    $manifest =array(

        'acceptable_sugar_flavors' => array('CE','PRO','CORP','ENT','ULT'),

        'acceptable_sugar_versions' => array(

            'exact_matches' => array(),

            'regex_matches' => array('7\\.[0-9]\\.[0-9]\\.[0-9]$'),

        ),

        'author' => 'Simplifica',

        'description' => 'Logic Hook that sends data from SugarCRM to an URL',

        'icon' => '',

        'is_uninstallable' => true,

        'name' => 'Logic Hook URL',

        'published_date' => '2016-02-19 12:00:00',

        'type' => 'module',

        'version' => '1341607504',

    );

   

    $installdefs =array(

        'id' => 'package_1341607504',

        'copy' => array(

            0 => array(

                'from' => '<basepath>/Files/custom/modules/Contacts/contacts_save.php',

                'to' => 'custom/modules/Contacts/contacts_save.php',

            ),

        ),

        'logic_hooks' => array(

            array(

                'module' => 'Contacts',

                'hook' => 'after_save',

                'order' => 99,

                'description' => 'Create contact in URL',

                'file' => 'custom/modules/Contacts/contacts_save.php',

                'class' => 'Contact_URL',

                'function' => 'createURL',

            ),

        ),

    );

?>

 

 

And here's the contacts_save.php code (the file that perform the operation):

<?php

    if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

 

    class Contact_RN

    {

        function createRightNow($bean, $event, $arguments)

        {

            $url = 'http://myurl.com';

            $username = 'user';

            $password = 'password';

           

            // the body of the info

            $postData = array(

                'name' => array(

                    'first' => $bean->first_name,

                    'last' => $bean->last_name

                ),

                'title' => $bean->title

            );

           

            $info = json_encode($postData);

           

            $ch = curl_init();

            curl_setopt($ch, CURLOPT_HEADER, false);

            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

            curl_setopt($ch, CURLOPT_URL, $url);

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

            curl_setopt($ch, CURLOPT_POSTFIELDS, $info);

           

            curl_exec($ch);

               

            curl_close($ch);

        }

    }

?>

 

For security reasons I can't write the real URL, so I put an example (same way for user and password).

Outcomes