我正在尝试使用php连接到API。我不断收到错误消息。我在C:\ xampp \ htdocs \ CURLAPI内的index.php中有此代码 错误“ stdClass对象([err] =>处理来自请求的给定输入时出错)” 这里是API。 https://deepai.org/machine-learning-model/sentiment-analysis 这是我的密码
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
$ch = curl_init();
$url = "https://api.deepai.org/api/sentiment-analysis";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Api-Key: quickstart-QUdJIGlzIGNvbWluZy4uLi4K",
'text: ' . 'This is good'
));
$resp = curl_exec($ch);
if($e = curl_error($ch)) {
echo $e;
}
else {
$decoded = json_decode($resp);
print_r($decoded);
}
curl_close($ch);
?>
</body>
</html>
答案 0 :(得分:0)
您正在将'text'参数作为标头的一部分传递,您应该在其中将它们作为POST请求发送。试试这个(没试过!)
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Api-Key: quickstart-QUdJIGlzIGNvbWluZy4uLi4K"
));
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, [ 'text' => 'This is good' ]);