从PHP代码调用Watson API

从PHP代码调用Watson API,php,curl,watson,watson-dialog,Php,Curl,Watson,Watson Dialog,我试图使用curl从PHP代码中调用自然语言理解Watson API。我已成功地尝试从终端卷曲。它给出了执行此命令的一些结果: curl -u "my_username":"my_password" "https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze?version=2017-02-27&text=I%20still%20have%20a%20dream.&featur

我试图使用curl从PHP代码中调用自然语言理解Watson API。我已成功地尝试从终端卷曲。它给出了执行此命令的一些结果:

curl -u "my_username":"my_password" "https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze?version=2017-02-27&text=I%20still%20have%20a%20dream.&features=sentiment,keywords"
但是,当我尝试使用以下代码在php中使用curl时:

<?php
$url = "https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze?version=2017-02-27&text=I%20still%20have%20a%20dream%2C%20a%20dream%20deeply%20rooted%20in%20the%20American%20dream%20%E2%80%93%20one%20day%20this%20nation%20will%20rise%20up%20and%20live%20up%20to%20its%20creed%2C%20%22We%20hold%20these%20truths%20to%20be%20self%20evident%3A%20that%20all%20men%20are%20created%20equal.&features=sentiment,keywords";
$post_args = array(
         'version' => '2017-02-27',
         'url' => 'https://davidwalsh.name/curl-post',
         'features' => 'sentiment,keywords' 
     );
$headers = array(
        'Content-Type:application/json'
    );
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, "my_username:my_password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//curl_setopt($ch, CURLOPT_POST, 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;
?>

该怎么办?

我不敢相信我被困在这里是因为一片空白。 替换此:

$headers = array(
        'Content-Type:application/json'
    );


成功了。请注意<代码>:和<代码>应用程序/JSON<代码> > < /P>您为什么要注释<代码> CurLopt*Prest< /C>和<代码> CurLoptHtHoppAth选项?另外,你从来没有发送过你的帖子。我尝试了你所拥有的代码,这对我来说很好。问题可能与版本有关。你有API文档的链接吗?@ChristianEsperar你得到了什么回应?你能在这里发布吗?@AnkitShubham类似于{“用法”:{“文本单位”:1,“文本字符”:21,“特征”:2},…}
$headers = array(
        'Content-Type:application/json'
    );
$headers = array(
        'Content-Type: application/json'
    );