大家好,我正在尝试将我的Web应用程序与PHP中的RESTful API集成。这需要我向https://api.api.ai/v1/query发送POST请求,并且还需要设置两个自定义头部。
我当前的代码是
$data = array("v" => "20150821", "query" => "whats the time", "lang" => "EN", "sessionId" => "1234567890"); $data_string = json_encode($data); $ch = curl_init('https://api.api.ai/v1/query');curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json',// 'Content-Length: ' . strlen($data_string), 'Content-type: application/json', 'Authorization: Bearer XxxX-xxx', 'ocp-apim-subscription-key: XxxX-xxx'));$result = curl_exec($ch);
如果我运行这段代码(当然是使用我的真实认证和密钥),我什么也得不到。就是空白。有什么想法吗?
回答:
非常抱歉。我最近睡眠不好,犯了一个相当愚蠢的错误——我忘了输出$result。
我知道,我知道。我以后会更加小心。无论如何,谢谢你 😛