Home  >  Article  >  Backend Development  >  How to verify the four elements of bank card in PHP

How to verify the four elements of bank card in PHP

青灯夜游
青灯夜游Original
2022-10-21 17:49:501121browse

Verification method: 1. Apply for the bank card four-element detection interface and obtain the interface request Key; 2. Call the interface API to make a request, process the data and return the result; 3. Use "$content=juheHttpRequest($apiUrl , $paramstring,1);" Get the content returned by the interface; 4. Process the returned content according to its own business logic and print the processing results.

How to verify the four elements of bank card in PHP

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer

Bank based on PHP Card four-element verification API interface calling example

Preliminary preparation

  • Application interface

Bank card four-element detection interface application address:https://www.juhe.cn/docs/api/id/213?s=cpphpcn

Self-service application to activate the SMS API and obtain the interface request key. (Currently, the interface only supports enterprise users)

PHP request code example

/**
 * 聚合银行卡四元素校验API发起请求-PHP代码
 * 功能:检测输入的姓名、手机号码、身份证号码、银行卡号是否一致。
 */

// 请求的接口URL
$apiUrl = 'http://v.juhe.cn/verifybankcard4/query';

// 请求参数
$params = [
    'realname' => 'xxx', // 姓名
    'idcard' => 'xxx', // 身份证号码
    'bankcard' => 'xxx', // 银行卡号
    'mobile' => 'xxx', // 手机号码

    'key' => 'xxx', // 接口调用key,通过聚合平台申请开通
];
$paramsString = http_build_query($params);

// 发起接口请求
$response = juheHttpRequest($apiUrl, $paramsString, 1);

// 处理接口返回结果,根据自身业务逻辑修改处理
$paramstring = http_build_query($params);
$content = juheHttpRequest($apiUrl, $paramstring, 1);
$result = json_decode($content, true);
if ($result) {
    if ($result['error_code'] == 0) {
        // 请求成功,根据自身业务逻辑修改处理
        $res = $result['result']['res'];
        if ($res == '1') {
            // 信息核验一致
            echo "信息核验一致";
        } else {
            // 信息核验不一致
            echo "信息核验不一致";
        }
    } else {
        // 请求异常,根据自身业务逻辑修改处理
        echo "{$result['error_code']}:{$result['reason']}" . PHP_EOL;
    }
} else {
    //可能网络异常等问题请求失败,根据自身业务逻辑修改处理
    echo "请求失败";
}

/**
 * 发起网络请求函数
 * @param string $url 请求的URL
 * @param bool $params 请求的参数内容
 * @param int $ispost 是否POST请求
 * @return bool|string 返回内容
 */
function juheHttpRequest($url, $params = false, $ispost = 0)
{
    $httpInfo = [];
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'JUHE API');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
    curl_setopt($ch, CURLOPT_TIMEOUT, 12);
    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;
}

For detailed request parameter and return parameter format instructions, please Refer to the official interface document: https://www.juhe.cn/docs/api/id/213?s=cpphpcn

Recommended learning: "PHP Video Tutorial

The above is the detailed content of How to verify the four elements of bank card in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn