使用官方 Demo 代碼調用圖靈機器人接口,PHP 端無法正常獲取回覆。代碼涉及 file_get_contents 請求 API、json_decode 解析、依 code 分類處理回覆內容(文字、鏈接、航班等)。
$key 為有效的圖靈 Key。$content 需 urlencode 後拼接。$url = 'https://www.tuling123.com/openapi/api?key=' . $key . '&info=' . urlencode($content);
$response = @file_get_contents($url);
if ($response === FALSE) {
// 記錄錯誤,返回預設回覆
return '機器人暫時無法連線,請稍後再試。';
}
$re = json_decode($response, true);
if (!$re || !isset($re['code'])) return '回覆格式異常。';
// 後續 switch 處理各 code...
原提問者後續回覆「已解決,是低級錯誤」,常見為 Key 錯誤或編碼遺漏。