<?php
function get_access_token(){
$url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=XXXX&client_secret=XXXXXXXX";
$options = [
'http' => [
'header' => "Content-type: application/json",
'method' => 'POST',
]
];
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
if ($response === false) {
// 请求失败
die('请求失败');
}
// 返回的响应是JSON格式,你可以解析它
$result = json_decode($response, true);
return $result['access_token'];
}
$token = get_access_token();
$url2 = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=".$token;
$options = [
'http' => [
'header' => "Content-type: application/json",
'method' => 'POST',
'content' => '{"messages": [{"role": "user", "content": "介绍一下你自己"}]}'
]
];
$context = stream_context_create($options);
$response = file_get_contents($url2, false, $context);
if ($response === false) {
// 请求失败
die('请求失败');
}
// 返回的响应是JSON格式,你可以解析它
$result = json_decode($response, true);
// 打印解析后的结果
var_dump($result);
php使用文心一言api接口
于 2023-09-04 14:27:56 首次发布