Skip to main content

Post data from your website to Kintone to adding Kintone app record by PHP

 When you want to add your PHP website data to Kintone you have two choices to do this.

First way, you can use jquery ajax to post data from your website to Kintone app

=> with this choice you take CORS error if you don't add headers parameter right

Second way, You can use curl to post data from PHP. This way is good with simple headers params.

I was successful with this way. Then I'm going to show you how to do this.

In this article I used Laravel to post data by CURL.

OK, firstly you create a form to input datas.

<h2><strong>Test send data to Kintone</strong></h2>
<!-- Site wrapper -->
<div class="wrapper">
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper" style="min-height: 2644px;">
<!-- Main content -->
<section class="content">

<!-- Default box -->
<div class="card">
<div class="card-body row">
<div class="col-7">
<form method="post" action="{{route('send')}}">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
<div class="form-group">
<label for="inputName">Name</label>
<input name="fullname" type="text" id="inputName" class="form-control">
</div>
<div class="form-group">
<label for="inputEmail">E-Mail</label>
<input name="email" type="email" id="inputEmail" class="form-control">
</div>
<div class="form-group">
<label for="inputSubject">Subject</label>
<input name="subject" type="text" id="inputSubject" class="form-control">
</div>
<div class="form-group">
<label for="inputMessage">Message</label>
<textarea name="message" id="inputMessage" class="form-control" rows="4"></textarea>
</div>
<div class="form-group">
<input type="submit" id="send" class="btn btn-primary" value="Send">
</div>
</form>

</div>
</div>
</div>

</section>
<!-- /.content -->
</div>

in POST action you include Controller to send data to Kintone

- Create a Controller to get data and send to Kintone by CURL

public function send(Request $request){
$name = $request->get('fullname');
$_token = $request->get('_token');
$email = $request->get('email');
$subject = $request->get('subject');
$message = $request->get('message');

$body = array(
'app'=> 'Kintone App ID',
'record'=>array(
'Name'=>array(
'value'=>$name
),
'Email'=>array(
'value'=>$email
),
'Subject'=>array(
'value'=>$subject
),
'Message'=>array(
'value'=>$message
),
)
);

$url = 'url to post data to your Kintone App';
$postdata = json_encode($body);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'X-Cybozu-API-Token: Your API Token'));
$result = curl_exec($ch);
curl_close($ch);

print_r ($result);
echo "<pre>";
die('OK');
}

In the route file (web.php) add code to make route to Controller

Route::post('/send','App\Http\Controllers\FormController@send')->name('send');

Refer to:

https://stackoverflow.com/questions/11079135/how-to-post-json-data-with-php-curl

Comments

Popular posts from this blog

Sample VS code setting

  When you are a software developer, you need to configure your IDE working with most convinience for your working, bellow is a sample code snippet to config your IDE {   "diffEditor.ignoreTrimWhitespace" : false ,   "javascript.updateImportsOnFileMove.enabled" : "always" ,   "[typescriptreact]" : {       "editor.defaultFormatter" : "esbenp.prettier-vscode"   },   "editor.formatOnPaste" : true ,   "workbench.settings.applyToAllProfiles" : [],   "editor.tabSize" : 2 ,   "redhat.telemetry.enabled" : true ,   "editor.codeActionsOnSave" : {       },   // "editor.codeActionsOnSave": {   //   "source.fixAll": "explicit",   //   "source.fixAll.eslint": "explicit",   //   "source.organizeImports": "explicit",   //   "source.sortMembers": "explicit",   //   "javascript.showUnused": "...

Docker Compose: Node.js Express and MongoDB example

  Docker   provides lightweight containers to run services in isolation from our infrastructure so we can deliver software quickly. In this tutorial, I will show you how to dockerize Nodejs Express and MongoDB example using   Docker Compose .