cURL输出整个响应

时间:2016-07-21 20:03:31

标签: php microsoft-cognitive

我正在使用Microsoft Congnitive WebLM来断言一组单词,并输出整个cURL请求,我无法弄清楚如何使用结果。

我正在使用的代码:

.location

我得到的结果是

$word = 'iwansttobreakfree';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
    "https://api.projectoxford.ai/text/weblm/v1.0/breakIntoWords?model=query&text=" . $word);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Ocp-Apim-Subscription-Key: [subscription key removed for stack overflow]"
));

$result = curl_exec($ch);

curl_close($ch);

var_dump($result);

有人知道如何使用结果json,而输出整个cURL响应吗?

2 个答案:

答案 0 :(得分:3)

您需要设置:

+---+-----+----+----+-------+---------+
|Age|Grade|Name|Sex |Subject|Unique_ID|
+---+-----+----+----+-------+---------+
| 16|   88|Tim |Male|Math   |        1|
+---+-----+----+----+-------+---------+

对此:

curl_setopt($ch, CURLOPT_HEADER, true);

enter image description here

查看文档:{​​{3}}

答案 1 :(得分:1)

我想通了,真是愚蠢的错误:/

通过将CURLOPT_RETURNTRANSFER设置为true并将CURLOPT_HEADER设置为false,将返回json字符串。

这是新代码:

$word = 'iwanttobreakfree';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
    "https://api.projectoxford.ai/text/weblm/v1.0/breakIntoWords?model=query&text=" . $word);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Ocp-Apim-Subscription-Key: [key removed for stack overflow]"
));

$result = curl_exec($ch);

curl_close($ch);

echo "\n--------\n";

$json = json_decode($result);
var_dump($json);

结果如下:

object(stdClass)#6 (1) {
  ["candidates"]=>
  array(5) {
    [0]=>
    object(stdClass)#1 (2) {
      ["words"]=>
      string(20) "i want to break free"
      ["probability"]=>
      float(-7.176)
    }
    [1]=>
    object(stdClass)#2 (2) {
      ["words"]=>
      string(19) "iwant to break free"
      ["probability"]=>
      float(-9.166)
    }
    [2]=>
    object(stdClass)#3 (2) {
      ["words"]=>
      string(19) "i want to breakfree"
      ["probability"]=>
      float(-9.547)
    }
    [3]=>
    object(stdClass)#4 (2) {
      ["words"]=>
      string(21) "i wan t to break free"
      ["probability"]=>
      float(-9.839)
    }
    [4]=>
    object(stdClass)#5 (2) {
      ["words"]=>
      string(16) "iwanttobreakfree"
      ["probability"]=>
      float(-9.877)
    }
  }
}