Post API via php file not working

I am new to php and APIs and am looking to insert a lead through a contact form in a Wordpress environment to a CRM Apptivo. Could anyone let me know why the code below is not working?

<?php
define ( 'APPTIVO_API_URL' , 'https://api2.apptivo.com/app/dao/v6/leads' );
define ( 'APPTIVO_API_KEY' ,
'xxx' );
define ( 'APPTIVO_ACCESS_KEY' , 'yyy' );


add_action( 'gform_after_submission_2', 'post_to_third_party_Apptivo', 10, 2 );
function post_to_third_party_Apptivo( $entry, $form ) {
 
    $post_url = 'APPTIVO_API_URL';
	$apiKey = 'APPTIVO_API_KEY';
	$accessKey = 'APPTIVO_ACCESS_KEY';
    $body = array(
        'lastName' => rgar( $entry, '8' ),
        'phoneNumber' => rgar( $entry, '5' ),
		'emailAddress' => rgar( $entry, '3' ),
		'referredByName' => 'flooringbrokers.ca',
		'leadStatus' => 'New',
        'Description' => rgar( $entry, '6' ),
        );
    GFCommon::log_debug( 'gform_after_submission: body => ' . print_r( $body, true ) );
 
	$params = array(
		"a" => "save" ,
		"leadData" => $body ,
		"apiKey" => APPTIVO_API_KEY ,
		"accessKey" => APPTIVO_ACCESS_KEY
		);
    $request = new WP_Http();
    $response = $request->post( $post_url, $params );
	
    GFCommon::log_debug( 'gform_after_submission: response => ' . print_r( $response, true ) );
}

?>

Open in new window

LVL 1
ShawnAsked:
Who is Participating?
 
Julian HansenConnect With a Mentor Commented:
First up - neaten up your code. Badly formatted code leads to bad thinking and bugs.

Second, point your request url to a reflection script to see what is being sent.

A reflection script is one that dumps (file / screen / both) data that was sent to it. For example
<h2>GET</h2>
<?php echo "<pre>" . print_r($_GET, true) . "</pre>";?>
<h2>POST</h2>
<?php echo "<pre>" . print_r($_POST, true) . "</pre>";?>

Open in new window

The above is a very simple script - you can expand to include Cookies, Headers, Files etc as required.
The above outputs to screen but if you can't view screen outputs change to using a file
file_put_contents('reflect.log', print_r($_GET, true), FILE_APPEND);

Open in new window


Let's verify that what is being sent is what we want to be sent.

Next step is to test the request using a tool like Postman to check that the request to the API is working with the data you are sending.

When both of the above have been verified you should have resolved the issue.
0
 
ShawnAuthor Commented:
thx & sorry for dropping the ball
0
All Courses

From novice to tech pro — start learning today.