下面是一个接口转发功能,只需匹配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;
?>