I researched everywhere and can't figure it out.
I am writing a cUrl test request to test my REST service:
$ch = curl_init();
$data = array(
"products" => array ("product1"=>"abc","product2"=>"pass"));
$data = json_encode($data);
$postArgs = 'order=new&data=' . $data;
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArgs);
curl_setopt($ch, CURLOPT_URL, 'http://localhost/store/rest.php');
curl_exec($ch);
This works fine, and the request is accepted by my service, and $ _Post is populated as necessary with two variables, order and data. Data has an encoded JSON object. And when I print $ _Post ['data'], it shows:
{"products":{"product1":"abc","product2":"pass"}}
This is exactly what is expected and identical to what was sent.
When I try to decode this, json_decode () returns nothing!
If I create a new line and manually type this line, json_decode () works fine!
I tried:
strip_tags(), , http
utf8_encode() utf 8
addslashes(),
.
, json_decode() , http-?
:
public static function processRequest($requestArrays) {
$request_method = strtolower($requestArrays->server['REQUEST_METHOD']);
$return_obj = new RestRequest();
$data = array();
switch ($request_method) {
case 'post':
$data = $requestArrays->post;
break;
}
$return_obj->setMethod($request_method);
$return_obj->setRequestVars($data);
if (isset($data['data'])) {
$return_obj->setData(json_decode($data['data']));
}
return $return_obj;
}