AnsweredAssumed Answered

Simple Rest API in logic hook

Question asked by Daniel Penner on May 13, 2016
Latest reply on May 16, 2016 by Matt Marum

I am using the rest API in a logic hook: process_record_method in order to simply add a module record to our Machines module (mach_Machine). I used the example given by sugar and then changed the variables to my own. After browsing to this page, the code process correctly and a record appears in the related module, but I get a 500 internal server error. I need this code to work but without the internal server error. Does anyone know what I did wrong? Sorry for the code in plain text, couldn't use raw HTML lines.

 

I created this .php page in: /custom/modules/mach_Machine/process_api.php

and a link to this page in: /custom/Extension/modules/mach_Machine/Ext/LogicHooks/logic_hook_process_record.php

 

  • I did a quick repair and rebuild before browsing to the page
  • I made sure both files are owned by apache
  • I made sure both files have full permissions (777)
  • After processing the code and getting the internal server error, I deleted the Rest API code (but I left the logic hook in place), uploaded it again and browsed to the Machines module once more and saw that a module was added correctly. (so the API code is working and the logic hook is working).
  • I replaced the username, password, client id, and secret id in the code below for security
  • This is the same code in the example that can be found here: http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.6/API/Web_Services/Examples/v10/module…

 

Here is my code: (It's worth noting that I tried to use the raw HTML and PHP syntax highliting, but it wasn't letting me copy-paste it in correctly)

<?php

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

require_once( 'include/SugarQuery/SugarQuery.php' );

 

class Process_API {

 

    function process_record_method( $bean, $event, $arguments ) {

    //_ppl( "yay, it worked!"); //this outputs to the sugarcrm.log file

    //_ppl($bean->toArray(0,1));

 

        $base_url = "http://crmdev.icee.com/rest/v10";

        $username = "[username]";

        $password = "[password]";

 

        /**

         * Generic function to make cURL request.

         * @param $url - The URL route to use.

         * @param string $oauthtoken - The oauth token.

         * @param string $type - GET, POST, PUT, DELETE. Defaults to GET.

         * @param array $arguments - Endpoint arguments.

         * @param array $encodeData - Whether or not to JSON encode the data.

         * @param array $returnHeaders - Whether or not to return the headers.

         * @return mixed

         */

        function call(

            $url,

            $oauthtoken='',

            $type='GET',

            $arguments=array(),

            $encodeData=true,

            $returnHeaders=false

        )

        {

            $type = strtoupper($type);

 

            if ($type == 'GET')

            {

                $url .= "?" . http_build_query($arguments);

            }

 

            $curl_request = curl_init($url);

 

            if ($type == 'POST')

            {

                curl_setopt($curl_request, CURLOPT_POST, 1);

            }

            elseif ($type == 'PUT')

            {

                curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "PUT");

            }

            elseif ($type == 'DELETE')

            {

                curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "DELETE");

            }

 

            curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);

            curl_setopt($curl_request, CURLOPT_HEADER, $returnHeaders);

            curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);

            curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);

            curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);

 

            if (!empty($oauthtoken))

            {

                $token = array("oauth-token: {$oauthtoken}");

                curl_setopt($curl_request, CURLOPT_HTTPHEADER, $token);

            }

 

            if (!empty($arguments) && $type !== 'GET')

            {

                if ($encodeData)

                {

                    //encode the arguments as JSON

                    $arguments = json_encode($arguments);

                }

                curl_setopt($curl_request, CURLOPT_POSTFIELDS, $arguments);

            }

 

            $result = curl_exec($curl_request);

 

            if ($returnHeaders)

            {

                //set headers from response

                list($headers, $content) = explode("\r\n\r\n", $result ,2);

                foreach (explode("\r\n",$headers) as $header)

                {

                    header($header);

                }

 

                //return the nonheader data

                return trim($content);

            }

 

            curl_close($curl_request);

 

            //decode the response from JSON

            $response = json_decode($result);

 

            return $response;

        }

 

        //Login - POST /oauth2/token

 

        $url = $base_url . "/oauth2/token";

 

        $oauth2_token_arguments = array(

            "grant_type" => "password",

            //client id/secret you created in Admin > OAuth Keys

            "client_id" => "[client ID]",

            "client_secret" => "[client secret]",

            "username" => $username,

            "password" => $password,

            "platform" => "base"

        );

 

        $oauth2_token_response = call($url, '', 'POST', $oauth2_token_arguments);

 

        //Create record - POST /<module>/

 

        $url = $base_url . "/mach_Machine";

 

        $record_arguments = array(

            "name" => "New Machine",

            "description" => "Description of my Machine",

            "machine_order_number_c" => "58"

        );

 

        $record_response = call($url, $oauth2_token_response->access_token, 'POST', $record_arguments);

 

        _ppl($record_response);

        //echo "<pre>";

        //print_r($record_response);

        //echo "</pre>";

    }

 

}

Outcomes