Add the Do you like me widget to your website

Original link: https://5ime.cn/doyoulikeme.html

Someone should have noticed that the blogger added a Do You Like Me widget to the sidebar. In fact, I think the easier way is to use PHP+SQL+AJAX , but in the idea of ​​tossing + reducing costs (white prostitution) , using Vercel , LeandCloud and Node.JS to roll one out.

Node version

Since Node.Js is in a semi-entry state, the code quality is not high, but the program runs in a strange way after all.

Project address: https://github.com/5ime/likeMe

PHP version

Since I am hosted by vercel do not have permission to创建or修改files, I will request LeanCloud every time I get the number of like , resulting in a high number of requests, so I created a PHP version and wrote the number of like into the count.dat file. , read count.dat when requested and update count.dat after clicking. The following code can be uploaded to your own server.


composer install dependencies

 1
 composer require leancloud/leancloud-sdk

The specific code is as follows, please refer to https://github.com/5ime/likeMe/参数获取and组件调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
twenty one
twenty two
twenty three
twenty four
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 <?php
error_reporting ( 0 );
header ( 'Access-Control-Allow-Origin:*' );
header ( 'Content-type: application/json' );

require_once ( "vendor/autoload.php" );

use LeanCloud \ Query ;
use LeanCloud \ LeanObject ;
use LeanCloud \ CloudException ;

// 参数依次为app-id, app-key, master-key
LeanCloud\ Client :: initialize ( "你的app-id" , "你的app-key" , "你的master-key" );
// LeanCloud\Client::setDebug(true);

$file = fopen ( "count.dat" , "r+" );

if ( $_SERVER [ "REQUEST_METHOD" ] == "POST" ) {
$query = new Query ( "likeCount" );
// 修改为你的objectId
$todo = $query -> get ( "你的objectId" );
$data = $todo -> increment ( "count" , 1 );

try {
$todo -> save ();
} catch (CloudException $e ) {
echo $e -> getMessage ();
}

fwrite ( $file , $data -> get ( "count" ));
$Json = array (
'code' => 200 ,
'msg' => 'success'
);
} else {
$count = fread ( $file , filesize ( "count.dat" ));
$Json = array (
'code' => 200 ,
'msg' => 'success' ,
'data' => array (
'count' => $count
)
);
}

fclose ( $file );
$Json = json_encode ( $Json ,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
echo stripslashes ( $Json );
return $Json ;

This article is reprinted from: https://5ime.cn/doyoulikeme.html
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment