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

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 .

MUI dialog make sure user can't click on outside when you're opening dialog

< Dialog       open = { open }       onClose = { ( _event , reason ) => {         if ( reason === "backdropClick" || reason === "escapeKeyDown" ) {           return ;         }         onClose ?.();       } }       maxWidth = { maxWidth }       fullWidth       PaperProps = { {         className : "rounded-lg shadow-lg" ,       } }       disableEscapeKeyDown     > { if (reason === "backdropClick" || reason === "escapeKeyDown") { return; } onClose?.(); }} maxWidth={maxWidth} fullWidth PaperProps={{ className: "rounded-lg shadow-lg", }} disableEscapeKeyDown >

Laravel API Tutorial: How to Build and Test a RESTful API

  Laravel is a PHP framework developed with developer productivity in mind. Written and maintained by Taylor Otwell, the framework is very opinionated and strives to save developer time by favoring convention over configuration. The framework also aims to evolve with the web and has already incorporated several new features and ideas in the web development world—such as job queues, API authentication out of the box, real-time communication, and much more. In this article, we’ll explore the ways you can build—and test—a robust API using Laravel. We’ll be using Laravel 5.4, and all of the code is available for reference on GitHub.