搭建一个属于自己的简易版“一言”接口

66次阅读
没有评论

搭建一个属于自己的简易版“一言”接口

说明

由于个人使用的是handsome模板主题,所以以下的操作皆建立在该主题的基础上。如果要引用的话,请根据自己的实际情况进行修改。

接口搭建

PHP代码

<span class="php"><span class="hljs-meta"><?php</span>
<span class="hljs-comment">//获取句子文件的绝对路径</span>
<span class="hljs-comment">//如果你介意别人可能会拖走这个文本,可以把文件名自定义一下,或者通过Nginx禁止拉取也行。</span>
$path = dirname(<span class="hljs-keyword">__FILE__</span>);
$file = file($path.<span class="hljs-string">"/yiyan.txt"</span>);

<span class="hljs-comment">//随机读取一行</span>
$arr  = mt_rand( <span class="hljs-number">0</span>, count( $file ) - <span class="hljs-number">1</span> );
$content  = trim($file[$arr]);

<span class="hljs-comment">//编码判断,用于输出相应的响应头部编码</span>
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">isset</span>($_GET[<span class="hljs-string">'charset'</span>]) && !<span class="hljs-keyword">empty</span>($_GET[<span class="hljs-string">'charset'</span>])) {
    $charset = $_GET[<span class="hljs-string">'charset'</span>];
    <span class="hljs-keyword">if</span> (strcasecmp($charset,<span class="hljs-string">"gbk"</span>) == <span class="hljs-number">0</span> ) {
        $content = mb_convert_encoding($content,<span class="hljs-string">'gbk'</span>, <span class="hljs-string">'utf-8'</span>);
    }
} <span class="hljs-keyword">else</span> {
    $charset = <span class="hljs-string">'utf-8'</span>;
}
header(<span class="hljs-string">"Content-Type: text/html; charset=$charset"</span>);

<span class="hljs-comment">//格式化判断,输出js或纯文本</span>
<span class="hljs-keyword">if</span> ($_GET[<span class="hljs-string">'format'</span>] === <span class="hljs-string">'js'</span>) {
    <span class="hljs-keyword">echo</span> <span class="hljs-string">"function hitokoto(){document.write('"</span> . $content .<span class="hljs-string">"');}"</span>;
} <span class="hljs-keyword">else</span> {
    <span class="hljs-keyword">echo</span> $content;
}
</span>

txt

将txt的名字与上方代码中的名字相对应,并且与php处于同一目录下。格式如下:

搭建一个属于自己的简易版“一言”接口

接口使用

我自己使用的模板接口引用修改路径位于网站根目录下/usr/themes/handsome/index.php修改index.php文件

原始代码

<span class="hljs-comment"><!--/公告位置--></span>
          <span class="php"><span class="hljs-meta"><?php</span> <span class="hljs-keyword">endif</span>; <span class="hljs-meta">?></span></span>
        <span class="hljs-tag"><<span class="hljs-name">header</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"bg-light lter b-b wrapper-md"</span>></span>
          <span class="hljs-tag"><<span class="hljs-name">h1</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"m-n font-thin h3 text-black l-h"</span>></span><span class="php"><span class="hljs-meta"><?php</span> <span class="hljs-keyword">$this</span>->options->title(); <span class="hljs-meta">?></span></span><span class="hljs-tag"></<span class="hljs-name">h1</span>></span>
          <span class="hljs-tag"><<span class="hljs-name">small</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text-muted letterspacing indexWords"</span>></span><span class="php"><span class="hljs-meta"><?php</span>
              <span class="hljs-keyword">if</span> (@!in_array(<span class="hljs-string">'hitokoto'</span>,<span class="hljs-keyword">$this</span>->options->featuresetup)) {
                  <span class="hljs-keyword">$this</span>->options->Indexwords();
              }<span class="hljs-keyword">else</span>{
                  <span class="hljs-keyword">echo</span> <span class="hljs-string">'加载中……'</span>;
                  <span class="hljs-keyword">echo</span> <span class="hljs-string">'<script>
                         $.ajax({
                            type: \'Get\',
                            url: \'https://v1.hitokoto.cn/\',
                            success: function(data) {
                               var hitokoto = data.hitokoto;
                              $(\'.indexWords\').text(hitokoto);
                            }
                         });
</script>'</span>;
              }
              <span class="hljs-meta">?></span></span><span class="hljs-tag"></<span class="hljs-name">small</span>></span>
          <span class="hljs-tag"></<span class="hljs-name">header</span>></span>
        <span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"wrapper-md"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"post-panel"</span>></span>

修改之后

        <span class="hljs-comment"><!--/公告位置--></span>
    <span class="php"><span class="hljs-meta"><?php</span> <span class="hljs-keyword">endif</span>; <span class="hljs-meta">?></span></span>
    <span class="hljs-tag"><<span class="hljs-name">header</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"bg-light lter wrapper-md"</span>></span>
        <span class="hljs-tag"><<span class="hljs-name">h1</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"m-n font-thin text-black l-h"</span>></span><span class="php"><span class="hljs-meta"><?php</span> <span class="hljs-keyword">$this</span>->options->title(); <span class="hljs-meta">?></span></span><span class="hljs-tag"></<span class="hljs-name">h1</span>></span>
        <span class="hljs-tag"><<span class="hljs-name">small</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text-muted letterspacing indexWords"</span>></span>
            <span class="php"><span class="hljs-meta"><?php</span> $hitokoto = file_get_contents(<span class="hljs-string">'https://www.ndmiao.cn/API/yiyan'</span>); <span class="hljs-meta">?></span></span>
  <span class="php"><span class="hljs-meta"><?php</span> <span class="hljs-keyword">echo</span> $hitokoto; <span class="hljs-meta">?></span></span><span class="hljs-tag"></<span class="hljs-name">small</span>></span>
    <span class="hljs-tag"></<span class="hljs-name">header</span>></span>
    <span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"wrapper-md"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"post-panel"</span>></span>

根据自己的情况进行一些必要的修改即可

初壹先生
版权声明:本站原创文章,由初壹先生2021-09-25发表,共计1631字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
载入中...