0
votes

I have 2 sites.

  • Sita A runs Wordpress and has Gravity Forms.
  • Site B is attempting to use the Gravity Forms Web API to add an entry to Gravity Forms in Site A

Regardless of what I try, the best result I can get is a "resource created" response (201) with an empty response object. Regardless of the success response, no record is created.

{"status":201,"response":[]}

I can successfully GET data so I don't think it's an auth issue. The documentation says the response should contain "A localized message describing the result: "Entries created successfully"".

I've tried pointing at /entries as well as /forms/$form_id/entries

I installed the Web API Client plugin and successfully created a record with that. I've also poured over that code a thousand times. That plugin uses wp_remote_request to post the data which I can't use because I'm not running Wordpress.

Here's the current code:

<?php
$api_key = '123';
$private_key = 'abc';
$method  = 'POST';
$endpoint = 'http://example.com/gravityformsapi/';
$route = 'entries';
//$route = 'forms/17/entries';
$expires = strtotime('+60 mins');
$string_to_sign = sprintf('%s:%s:%s:%s', $api_key, $method, $route, $expires);
$sig = calculate_signature($string_to_sign, $private_key);

$api_call = $endpoint.$route.'?api_key='.$api_key.'&signature='.$sig.'&expires='.$expires;

$data = array(
    "id" => "",
    "date_created" => "",
    "form_id" => "17",
    "1" => "[email protected]"
);

$ch = curl_init($api_call);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);

$result = curl_exec($ch);

print_r($result);
1

1 Answers

2
votes

Your code is close. I used json_encode instead of http_build_query.

You might want to check your lead table for some orphaned entries.

Here is some code that is working for me:

<?php
$api_key = 'keyhere';
$private_key = 'privatekey';
$method  = 'POST';
$endpoint = 'https://www.site.com/gravityformsapi/';
//$route = 'entries';
$route = 'forms/1/entries';
$expires = strtotime('+60 mins');
$string_to_sign = sprintf('%s:%s:%s:%s', $api_key, $method, $route, $expires);
$sig = calculate_signature($string_to_sign, $private_key);

$api_call = $endpoint.$route.'?api_key='.$api_key.'&signature='.$sig.'&expires='.$expires;

//array of entries (each entry is an array with key=field id)
$entries = array(
    array("status"=>"active","1.3"=>"firstname","1.6"=>"lastname","2"=>"","3"=>"[email protected]","4"=>"testtest","5"=>"something else")
);

$ch = curl_init($api_call);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($entries));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);

$result = curl_exec($ch);

print_r($result);//201 status indicates it inserted the entry. Should return id of the entry.

function calculate_signature($string, $private_key) {
    $hash = hash_hmac("sha1", $string, $private_key, true);
    $sig = rawurlencode(base64_encode($hash));
    return $sig;
}
?>