php version: filter out sensitive words such as advertisements

Posted by jamie85 on Fri, 11 Oct 2019 00:28:28 +0200

Now the network is still very messy, especially full of all kinds of **, **, and ** related content, it is obvious that this is not in accordance with our country's law, so in order to a product can live a long and healthy life, it is better to use a certain strategy to filter or remind users not to send such content. But it's easier said than done, and it's still difficult to implement. The simplest way is to call the existing interface. Take php as an example:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<?php

/**
 * @author 
 * @copyright 2019
 */
 
header("content-type:text/html;charset=utf-8");         //Set encoding
 
//Configure the appKey and openId you applied for
$app_key = "***";
$open_id = "***";

/**
$url Request address
$params Request parameters
$ispost Request method
*/

function http_curl($url,$params=false,$ispost=false){
   
    $httpInfo = array();
    $ch = curl_init();

    curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
    curl_setopt( $ch, CURLOPT_USERAGENT , "xiaocongjisuan");
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
    curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
    
    if( $ispost )
    {
        curl_setopt( $ch , CURLOPT_POST , true );
        curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
        curl_setopt( $ch , CURLOPT_URL , $url );
    }
    else
    {
        if($params){
            curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
        }else{
            curl_setopt( $ch , CURLOPT_URL , $url);
        }
    }
    
    $response = curl_exec( $ch );
    if ($response === FALSE) {
        //echo "cURL Error: " . curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    curl_close( $ch );
    
    return $response;
}

function main(){
    
    global $app_key;
    global $open_id;
    
    $domain="http://api.xiaocongjisuan.com/";
    $servlet="data/taboo/recognize";
    $method="get";
    
    $url=$domain."".$servlet;
    
    $params['appKey']=$app_key;
    $params['openId']=$open_id;
    
    //Variable part
    $params["content"]="Please don't leave your buttons to find your partner.";
    $params["type"]="ad";
    
    //Code conversion
    foreach ($params as $key=>$value) {
        $params[$key]=mb_convert_encoding($value, "UTF-8", "GBK");
    }

    $paramstring = http_build_query($params);
    $content = http_curl($url,$paramstring,true);
    
    return $content;
}

echo main();
?>

Obviously this approach is very simple, and if you don't like to use php, you can use other languages. Take python for example:

# -*- coding: utf-8 -*-
# flake8: noqa
__author__ = 'wukong'

import urllib
from urllib import urlencode

#Configure the appKey and openId you applied for
app_key="***"
open_id="***"

"""
request_url Request address
params Request parameters
method Request method

"""
def request_content(request_url,params,method):
    params = urlencode(params)
    
    if method and method.lower() =="get":
        f = urllib.urlopen("%s?%s" % (request_url, params))
    else:
        f = urllib.urlopen(request_url, params)
 
    content = f.read()
    print content

   
def main():

    domain="http://api.xiaocongjisuan.com/";
    servlet="data/taboo/recognize"
    method="get"
    request_url=domain+servlet
    
    #Dictionaries
    params ={}
    params["appKey"]=app_key
    params["openId"]=open_id
    
    #Variable part
    params["content"]="Please don't leave your buttons to find your partner."
    params["type"]="ad"
    
    request_content(request_url,params,method)
    
if __name__ == '__main__':
    main()

As for the implementation of other languages, there are ready-made code, you can Jump here Check it out.

Topics: Programming PHP network encoding curl