php使用文心一言api接口

 [
			'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,php)