2015-06-20 83 views
0

我想通過POST發送curl數據。PHP curl post解析錯誤

這裏是我做了什麼

$ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,"https://lc-api.sdl.com/translate"); 
    $headers = array(); 
    $headers[] = 'Content-type:application/json'; 
    $headers[] = 'Authorization:LC apiKey=QidCWRGQ%2BHsdzGE6rrBgiw%3D%3D'; 

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS,"text=Bonjour&from=fra&to=eng"); //Post Fields 
    $server_output = curl_exec ($ch); 

    curl_close ($ch); 

    print $server_output ;  

我得到指示

Error parsing json at column:6 line:1 offset:-1 

這裏的錯誤是工作捲曲請求

curl -X POST -H "Content-type: application/json" -H "Authorization: LC apiKey=QidCWRGQ%2BHsdzGE6rrBgiw%3D%3D" -d '{"text":"Bonjour", "from" : "fra", "to" : "eng"}' https://lc-api.sdl.com/translate 

回答

2

嘗試如下:

<?php 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,"https://lc-api.sdl.com/translate"); 
$headers = array(); 
$headers[] = 'Content-type:application/json'; 
$headers[] = 'Authorization:LC apiKey=QidCWRGQ%2BHsdzGE6rrBgiw%3D%3D'; 

$data = array("text" => "Bonjour", "from" => "fra", "to" => "eng"); 
$data_string = json_encode($data); 

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string); //Post Fields 
$server_output = curl_exec ($ch); 

curl_close ($ch); 

print $server_output ; 
?> 

格式爲json_encode的後欄。

+0

我收到了同樣的錯誤,即使我改變了它 – user2800040

+0

我'{ 「partialTranslation」:假 「從」:「FRA 「,」to「:」eng「,」wordCount「:1,」charCount「:7,」translation「:」Hello「}'結果。這是根據您的要求得出的結果嗎?清除瀏覽器緩存,然後檢查。 – AnkiiG

+0

是啊ankit這是所需的結果 – user2800040

0

地址:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 

全碼:

<?php 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,"https://lc-api.sdl.com/translate"); 
$headers = array(); 
$headers[] = 'Content-type:application/json'; 
$headers[] = 'Authorization:LC apiKey=QidCWRGQ%2BHsdzGE6rrBgiw%3D%3D'; 

$data = array("text" => "Bonjour", "from" => "fra", "to" => "eng"); 
$data_string = json_encode($data); 

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string); //Post Fields 
$server_output = curl_exec ($ch); 

curl_close ($ch); 

var_dump($server_output); 
?>