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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ShawnAuthor Commented:
thx & sorry for dropping the ball
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.