Php cURL输出整个响应

Php cURL输出整个响应,php,microsoft-cognitive,Php,Microsoft Cognitive,我正在使用MicrosoftCongnitiveWebLM对一组单词进行分词,它输出整个cURL请求,我不知道如何使用结果 我正在使用的代码: $word = 'iwansttobreakfree'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://api.projectoxford.ai/text/weblm/v1.0/breakIntoWords?model=query&text=" . $word); c

我正在使用MicrosoftCongnitiveWebLM对一组单词进行分词,它输出整个cURL请求,我不知道如何使用结果

我正在使用的代码:

$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);
结果是我得到的是

    HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 320
Content-Type: application/json; charset=utf-8
Expires: -1
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
apim-request-id: ef73d48d-ab35-4fa5-a346-c57339415824
Date: Thu, 21 Jul 2016 20:00:12 GMT

{"candidates":[{"words":"i want to break free","probability":-7.1759999999999993},{"words":"iwant to break free","probability":-9.1660000000000021},{"words":"i want to breakfree","probability":-9.547},{"words":"i wan t to break free","probability":-9.8390000000000022},{"words":"iwanttobreakfree","probability":-9.877}]}
--------
bool(true)
有人知道我如何使用json结果,而不输出整个cURL响应吗?

您需要设置:

curl_setopt($ch, CURLOPT_HEADER, true);
为此:

curl_setopt($ch, CURLOPT_HEADER, false);

查看文档:

您需要设置:

curl_setopt($ch, CURLOPT_HEADER, true);
为此:

curl_setopt($ch, CURLOPT_HEADER, false);


查看文档:

我发现了一些非常愚蠢的错误:/

通过将
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)
    }
  }
}

我发现了,真是愚蠢的错误:/

通过将
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)
    }
  }
}

哈哈,我想这是一个
代码块
时刻,在喝了一杯啤酒后,很明显:)一直发生在我身上。哈哈,我想这是一个
代码块
时刻,在喝了一杯啤酒后,很明显:)一直发生在我身上。