如何使用 IBM Watson 的 Curl 获得 json 响应
How to get a json response using Curl from IBM Watson
我正在尝试编写一个简单的 Web 应用程序,它调用 IBM Watson NLC api 对用户将输入到文本框的文本进行分类。
我创建了以下内容,如果输入单个单词它就可以工作,如果给出一个句子就会失败。怎么了?任何帮助请。
输入语言为阿拉伯语。
文件 1 - NLCApp.php
<form action="post-method.php" method="POST">
<input type="text" name="texta" placeholder="Text to Query" multiple />
<input type="submit" name="submit" />
</form>
文件 2 - Postmethod.php
<?php
$wtext= ($_POST['texta']);
//echo $wtext;
$url = "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/{My Classifier ID}/classify?text=$wtext";
$headers = array('Content-Type:application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, '{My Classifier Username:My Classifier Password}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_args));
$result = curl_exec($ch);
if ($result === FALSE) {die('Send Error: ' . curl_error($ch)); }
curl_close($ch);
echo $result;
?>
我的编码很烂,大部分这些小代码都是从 Whosebug 和其他帮助论坛中的多个用户帖子复制粘贴的。
请提供有关完成此内容的任何帮助。
请试试这个:
$ch = curl_init($url);
- 删除未使用的 headers
- 添加错误处理
<?php
$wtext= ($_POST['texta']);
echo "Passed text: ".$wtext;
$url = "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/{My Classifier ID}/classify?text=";
$url .= urlencode($wtext);
// $headers = array('Content-Type:application/json');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, '{My Classifier Username:My Classifier Password}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_URL, $url);
// curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_args));
$result = curl_exec ($ch);
// also get the error and response code
$errors = curl_error($ch);
$response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
var_dump($result);
var_dump($errors);
var_dump($response);
?>
我正在尝试编写一个简单的 Web 应用程序,它调用 IBM Watson NLC api 对用户将输入到文本框的文本进行分类。
我创建了以下内容,如果输入单个单词它就可以工作,如果给出一个句子就会失败。怎么了?任何帮助请。
输入语言为阿拉伯语。
文件 1 - NLCApp.php
<form action="post-method.php" method="POST">
<input type="text" name="texta" placeholder="Text to Query" multiple />
<input type="submit" name="submit" />
</form>
文件 2 - Postmethod.php
<?php
$wtext= ($_POST['texta']);
//echo $wtext;
$url = "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/{My Classifier ID}/classify?text=$wtext";
$headers = array('Content-Type:application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, '{My Classifier Username:My Classifier Password}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_args));
$result = curl_exec($ch);
if ($result === FALSE) {die('Send Error: ' . curl_error($ch)); }
curl_close($ch);
echo $result;
?>
我的编码很烂,大部分这些小代码都是从 Whosebug 和其他帮助论坛中的多个用户帖子复制粘贴的。
请提供有关完成此内容的任何帮助。
请试试这个:
$ch = curl_init($url);
- 删除未使用的 headers
- 添加错误处理
<?php
$wtext= ($_POST['texta']);
echo "Passed text: ".$wtext;
$url = "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/{My Classifier ID}/classify?text=";
$url .= urlencode($wtext);
// $headers = array('Content-Type:application/json');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, '{My Classifier Username:My Classifier Password}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_URL, $url);
// curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_args));
$result = curl_exec ($ch);
// also get the error and response code
$errors = curl_error($ch);
$response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
var_dump($result);
var_dump($errors);
var_dump($response);
?>