2017-03-01 36 views
0

我知道有個人製作的圖書館(https://github.com/tgallice/wit-php)。但是,我無法找到他如何格式化捲曲。我只想要做一個請求,所以使用他的庫會過度。如何在PHP中格式化curl?

下面是在終端工作的字符串,但我不知道如何用PHP編寫:curl -H 'Authorization: Bearer ACCESSCODE' 'htps://api.wit.ai/message?v=20160526&q=mycarisbroken'

我試過,但它不工作:

$ch1 = curl_init(); 
curl_setopt($ch1, CURLOPT_URL,"htps://api.wit.ai/message?v=20160526&q=my%20car%20doesnt%20work"); 
curl_setopt($ch1, CURLOPT_POST, 1); 
// curl_setopt($ch1, CURLOPT_POSTFIELDS,$vars); //Post Fields 
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true); 

$headers = [ 
    'Authorization: Bearer ACCESSCODEOMITTED', 
]; 

curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers); 
$server_output = curl_exec ($ch1); 
curl_close($ch1); 

Data::$answer = json_decode($server_output)['entities']['intent'][0]['value']; 

回答

0

因爲我不能確定什麼是「不工作」代表,我改寫了所需終端命令PHP捲曲:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'https://api.wit.ai/message?v=20160526&q=mycarisbroken'); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ACCESSCODE')); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
$server_output = curl_exec($ch); 
curl_close($ch); 

注:有問題的URL有不正確htps協議,我在答案中將其固定爲https