php如何调用claude的api接口?
网友回复
首先也要登录claude的后台申请key
https://console.anthropic.com/settings/keys
php
<?php $ANTHROPIC_API_KEY = 'your_api_key'; // Replace with your actual API key $url = 'https://api.anthropic.com/v1/messages'; $data = array( "model" => "claude-3-5-sonnet-20240620", "max_tokens" => 1024, "messages" => array( array("role" => "user", "content" => "Hello, Claude") ) );
$data_string = json_encode($data); $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'x-api-key: ' . $ANTHROPIC_API_KEY, 'anthropic-version: 2023-06-01', 'content-type: application/json', 'Content-Length: ' . strlen($data_string)) ); $response = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } curl_close($ch); $json_response = json_decode($r...
点击查看剩余70%