浪迹༣ 2024-01-12 14:54 采纳率: 88.2%
浏览 0
已结题

接口转发时怎么给相应参数匹配不同数值

下面是一个接口转发功能,只需匹配uid,其他的参数采用直接赋值
需要实现功能:提取uid后,给userid、account和password赋值,如:
uid=88时,则userid=10、account=88、password=test1;
uid=99时,则userid=20、account=99、password=test2;
否则直接默认赋值userid=11、account=11、password=test;
赋值后转发至新接口

<?php
// 接收原始请求参数
$uid = $_REQUEST['uid']; // 用户ID
$mobile = $_REQUEST['mobile']; // 号码
$content = $_REQUEST['content']; // 内容
// 构造新接口的请求参数
$post_data = array(
    'userid' => 635,
  'account' => $uid,
    'password' => $pwd,
    'mobile' => $mobile,
    'content' => $content,
    'action' => 'send',
    'rt' => 'json'
);
// 构造请求字符串
$post_items = [];
foreach ($post_data as $key => $value) {
    $post_items[] = $key . '=' . urlencode($value);
}
$post_string = implode('&', $post_items);
// 初始化cURL
$url = 'http://www.alisms.cn/api';
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// 发送请求并获取结果
$result = curl_exec($ch);
curl_close($ch);

// 输出结果
echo 100;
?>

  • 写回答

5条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-01-12 14:54
    关注

    引自免费微信小程序:皆我百晓生

    可以使用switch语句来实现根据不同uid赋值不同的userid、account和password。下面是修改后的代码:

    <?php
    // 接收原始请求参数
    $uid = $_REQUEST['uid']; // 用户ID
    $mobile = $_REQUEST['mobile']; // 号码
    $content = $_REQUEST['content']; // 内容
    
    // 根据uid匹配不同的数值
    switch ($uid) {
        case '88':
            $userid = 10;
            $account = $uid;
            $password = 'test1';
            break;
        case '99':
            $userid = 20;
            $account = $uid;
            $password = 'test2';
            break;
        default:
            $userid = 11;
            $account = 11;
            $password = 'test';
            break;
    }
    
    // 构造新接口的请求参数
    $post_data = array(
        'userid' => $userid,
        'account' => $account,
        'password' => $password,
        'mobile' => $mobile,
        'content' => $content,
        'action' => 'send',
        'rt' => 'json'
    );
    
    // 构造请求字符串
    $post_items = [];
    foreach ($post_data as $key => $value) {
        $post_items[] = $key . '=' . urlencode($value);
    }
    $post_string = implode('&', $post_items);
    
    // 初始化cURL
    $url = 'http://www.alisms.cn/api';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    // 发送请求并获取结果
    $result = curl_exec($ch);
    curl_close($ch);
    
    // 输出结果
    echo $result;
    ?>
    

    在修改后的代码中,使用了switch语句根据$uid的值进行判断,并分别赋值给$userid$account$password。如果$uid是88,则赋值为10、88和test1;如果$uid是99,则赋值为20、99和test2;否则赋值为11、11和test。这样就可以根据不同的$uid匹配不同的数值了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 1月20日
  • 已采纳回答 1月12日
  • 创建了问题 1月12日

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计