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
Post a Comment