我们为具有重力形式的网站开发移动应用程序,我们可以通过重力形式的web api进行输入,但是在提交表单的网站中,它会自动创建新帖子,因为表单有一些帖子字段,当我们通过web api添加新条目,只需添加条目而不是帖子。 我们如何定义必须通过重力表格web api保存此条目?
更新
如果该表单包含帖子字段,那么重力表单可以通过它创建帖子。 read more
此功能仅适用于提交表单,因此您无法通过web api访问它。
为此,您必须手动添加
必须放在themes/YOUR_THEME/functions.php
的完整示例:
$api_key = 'your_public_key';
$private_key = 'your_private_key';
//set route
$route = 'entries'; // or 'forms/{formID}/entries'
//creating request URL
$expires = strtotime( '+1 day' );
$string_to_sign = sprintf( '%s:%s:%s:%s', $api_key, 'POST', $route, $expires );
$sig = calculate_signature( $string_to_sign, $private_key );
$url = 'http://your_domain.com/gravityformsapi/' . $route . '?api_key=' . $api_key . '&signature=' . $sig . '&expires=' . $expires;
if(isset($_POST)){
$body = [
[
"form_id"=> "1", // if $route is just 'entries'
'1' => "Post Title" ,
'2' => "Post Content"
]
//,[ another entry ]
];
$response = wp_remote_post($url,['body'=>json_encode($body)]);
if ( is_wp_error( $response ) ) {
wp_send_json_error($response->get_error_message());
} else {
$body = json_decode($response['body'],true);
if($body['status'] === 201){
foreach($body['response'] as $entry_id){ //get each entry ID
$entry = GFAPI::get_entry( $entry_id );
if(!is_wp_error($entry)){
$form = GFAPI::get_form( rgar($entry,'form_id') );
if($form)
GFCommon::create_post( $form, $entry); //Finally add Post.
else
wp_send_json_error("can not find entry form.");
}else
wp_send_json_error($entry->get_error_message());
}
wp_send_json_success("Success!");
}else{
wp_send_json_error($body);
}
}
}else
wp_send_json_error("request body invalid");
答案 0 :(得分:0)
$api_key = 'your_public_key';
$private_key = 'your_private_key';
//set route
$route = 'entries'; // or 'forms/{formID}/entries'
//creating request URL
$expires = strtotime( '+1 day' );
$string_to_sign = sprintf( '%s:%s:%s:%s', $api_key, 'POST', $route, $expires );
$sig = calculate_signature( $string_to_sign, $private_key );
$url = 'http://your_domain.com/gravityformsapi/' . $route . '?api_key=' . $api_key . '&signature=' . $sig . '&expires=' . $expires;
if(isset($_POST)){
$body = [
[
"form_id"=> "1", // if $route is just 'entries'
'1' => "Post Title" ,
'2' => "Post Content"
]
//,[ another entry ]
];
$response = wp_remote_post($url,['body'=>json_encode($body)]);
if ( is_wp_error( $response ) ) {
wp_send_json_error($response->get_error_message());
} else {
$body = json_decode($response['body'],true);
if($body['status'] === 201){
foreach($body['response'] as $entry_id){ //get each entry ID
$entry = GFAPI::get_entry( $entry_id );
if(!is_wp_error($entry)){
$form = GFAPI::get_form( rgar($entry,'form_id') );
if($form)
GFCommon::create_post( $form, $entry); //Finally add Post.
else
wp_send_json_error("can not find entry form.");
}else
wp_send_json_error($entry->get_error_message());
}
wp_send_json_success("Success!");
}else{
wp_send_json_error($body);
}
}
}else
wp_send_json_error("request body invalid");