PHP JSON卷曲问题

时间:2016-07-14 09:32:47

标签: php json curl

所以我试图使用CURL获取JSON返回但是我一直得到一个NULL值。

  

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);


?>

2 个答案:

答案 0 :(得分:0)

您正在重复此json_encode

暂时删除一个尝试$data_string = $data;

答案 1 :(得分: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);