Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP Microsoft认知服务中使用cURL获取HTTP POST请求的无效输入记录_Php_Curl_Post - Fatal编程技术网

在PHP Microsoft认知服务中使用cURL获取HTTP POST请求的无效输入记录

在PHP Microsoft认知服务中使用cURL获取HTTP POST请求的无效输入记录,php,curl,post,Php,Curl,Post,这让我快发疯了。使用微软的认知服务,我试图使用PHP和cURL向情绪分析RESTful服务发布一些文本。我觉得我已经按照指示走了一段路 但是我不能让我的代码工作 $body = array( "documents" => array( "language" => "en", "id" => "one", "text" => "Well, look, you look back at George W. Bush’s administrati

这让我快发疯了。使用微软的认知服务,我试图使用PHP和cURL向情绪分析RESTful服务发布一些文本。我觉得我已经按照指示走了一段路

但是我不能让我的代码工作

$body = array(
    "documents" => array(
    "language" => "en",
    "id" => "one",
    "text" => "Well, look, you look back at George W. Bush’s administration, he selected people who were very close to his family, to his father, and so you do see some of that. But it’s interesting.",
     ),
   );

$API_Endpoint ="https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment";
$headers = ['Content-Type: application/json','Ocp-Apim-Subscription-Key:{myactualkeyhere}',];

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TRANSFERTEXT, true);
$result = curl_exec($ch);
curl_close($ch);
echo json_encode($result);
我得到的错误是

"{\"code\":\"BadRequest\",\"message\":\"Invalid request\",\"innerError\":{\"code\":\"InvalidRequestContent\",\"message\":\"Missing input records.\"}}"

因此,经过深思熟虑和反复试验,我能够回答我自己的问题。起初,我认为问题出在我的头上,但我不断收到关于需要API键的错误,尽管它显然在头中。但这个问题最终证明是一个非常基本的PHP数组创建问题,我只是错过了,因为我是一个noob

正确的数组语法为:

                   $body = array (
                                  'documents' => 
                                  array (
                                    0 => 
                                    array (
                                      'language' => 'en',
                                      'id' => 'one',
                                      'text' => $text,
                                    ),
                                  ),
                                );

我希望这能帮助像我这样的noob在PHP中卷曲restfulapi端点。祝你好运

因此,经过深思熟虑和反复试验,我能够回答我自己的问题。起初,我认为问题出在我的头上,但我不断收到关于需要API键的错误,尽管它显然在头中。但这个问题最终证明是一个非常基本的PHP数组创建问题,我只是错过了,因为我是一个noob

正确的数组语法为:

                   $body = array (
                                  'documents' => 
                                  array (
                                    0 => 
                                    array (
                                      'language' => 'en',
                                      'id' => 'one',
                                      'text' => $text,
                                    ),
                                  ),
                                );
我希望这能帮助像我这样的noob在PHP中卷曲restfulapi端点。祝你好运