2016-07-14 489 views
0

所以即時嘗試使用CURL獲得JSON返回,但是我不斷得到一個NULL值。PHP JSON捲曲問題

POST變量{mgf = userData; apiKey = 123455678qwertyui}

<? 

$data = json_encode(array(
"mgf" => "userData", 
"apiKey" => "123455678qwertyui" 
)); 
$data_string = json_encode($data); 
$ch = curl_init('http://www.mgf.ltd.uk/software-test/api.php'); 
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)) 
); 
$result = curl_exec($ch); 
$result = json_decode($result); 
var_dump($result); 


?> 
+0

瓦爾傾倒之前的原始結果對它進行解碼,因爲它可能包含無效字符,最終在解碼後爲空。 – Tacsiazuma

回答

0

這裏試試這個

<?php 
$data = array(
"mgf" => "userData", 
"apiKey" => "123455678qwertyui" 
); 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, 'http://www.mgf.ltd.uk/software-test/api.php'); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "mgf=" . $data["mgf"] . "&apiKey=" . $data["apiKey"]); 


curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$server_output = curl_exec ($ch); 

curl_close ($ch); 
var_dump($server_output); 
+0

如果這是正確的,請將其標記爲正確! – Dan

0

您正在重複這個json_encode

只是刪除一個現在嘗試這樣$data_string = $data;

+0

仍爲NULL值 –

+0

嘗試完整的php標記'' – C2486

+0

仍爲NULL請將您的頭髮撕掉.. –