【问题标题】:Create new Gravity Forms entry from external site using GF WebAPI使用 GF WebAPI 从外部站点创建新的重力形式条目
【发布时间】:2014-11-13 13:23:24
【问题描述】:

我有 2 个网站。

  • Sita A 运行 Wordpress 并拥有 Gravity Forms。
  • 站点 B 正在尝试使用 Gravity Forms Web API 向站点 A 中的重力表单添加条目

无论我尝试什么,我能得到的最好结果是一个“资源创建”响应 (201),响应对象为空。无论成功响应如何,都不会创建任何记录。

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

我可以成功获取数据,所以我认为这不是身份验证问题。文档说响应应该包含“描述结果的本地化消息:“成功创建条目”。

我试过指向 /entries 以及 /forms/$form_id/entries

我安装了 Web API 客户端插件并成功地创建了一条记录。我也把那个代码倒了上千次。该插件uses wp_remote_request 用于发布我无法使用的数据,因为我没有运行 Wordpress。

这是当前代码:

<?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" => "test3@test.com"
);

$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);

【问题讨论】:

    标签: wordpress rest curl gravity-forms-plugin


    【解决方案1】:

    您的代码很接近。我使用json_encode 而不是http_build_query

    您可能需要检查潜在客户表中的一些孤立条目。

    下面是一些对我有用的代码:

    <?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"=>"test@test.com","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;
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      • 2017-06-10
      • 1970-01-01
      • 2022-07-12
      • 2021-11-28
      • 2018-03-31
      相关资源
      最近更新 更多