<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//腾讯AI模板 phpCI框架
class Aaa extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->helper(array('form', 'url','base','captcha','curl')); //鍔犺浇杈呭姪鍑芥暟
$this->load->model('db_model');
$this->load->library('session');
}
public function index() {
// 接口请求参数
$params = array(
'app_id' => '1106635333',
'time_stamp' => time(),
'nonce_str' => $this->createNonceStr(),
'session' => '10000',
'question' => '你叫啥',
'sign' => '',
);
// 应用密钥
$appkey = 'bECdWrby1OGeF25f';
// 计算sign参数(接口请求签名)
$params['sign'] = $this->getReqSign($params, $appkey);
// 得到所有请求参数
// var_dump($params);
$url = 'https://api.ai.qq.com/fcgi-bin/nlp/nlp_textchat';
$response = $this->doHttpPost($url, $params);
var_dump($response) ;
}
// getReqSign :根据 接口请求参数 和 应用密钥 计算 请求签名
// 参数说明
// - $params:接口请求参数(特别注意:不同的接口,参数对一般不一样,请以具体接口要求为准)
// - $appkey:应用密钥
// 返回数据
// - 签名结果
function getReqSign($params /* 关联数组 */, $appkey /* 字符串*/)
{
// 1. 字典升序排序
ksort($params);
// 2. 拼按URL键值对
$str = '';
foreach ($params as $key => $value)
{
if ($value !== '')
{
$str .= $key . '=' . urlencode($value) . '&';
}
}
// 3. 拼接app_key
$str .= 'app_key=' . $appkey;
// 4. MD5运算+转换大写,得到请求签名
$sign = strtoupper(md5($str));
return $sign;
}
//生成16位随机字符串
private function createNonceStr($length = 16)
{
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
// doHttpPost :执行POST请求,并取回响应结果
// 参数说明
// - $url :接口请求地址
// - $params:完整接口请求参数(特别注意:不同的接口,参数对一般不一样,请以具体接口要求为准)
// 返回数据
// - 返回false表示失败,否则表示API成功返回的HTTP BODY部分
function doHttpPost($url, $post_data)
{
$ch =curl_init();
$timeout = 300;
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_REFERER, "http://api.dushiyangguan.com/");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$res = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
return $res;
}
}
腾讯AI模板 php CI框架
最新推荐文章于 2024-05-24 09:53:20 发布