我正在尝试使用php连接到API。我一直收到一个错误。我在index.php中的C:\xampp\htdocs\CURLAPI "stdClass Object ( err => error processing给定来自请求的输入)“中有此代码。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>发布于 2020-11-01 23:57:53
您正在将'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' ]);https://stackoverflow.com/questions/64633774
复制相似问题