Saya sedang menulis demo API REST khusus; sekarang ia dapat mengembalikan angka dan string dalam demo saya, tetapi saya ingin mengembalikan objek JSON seperti API ISTIRAHAT lainnya.
Dalam demo saya, saya memanggil Magento 2 API (yaitu mendapatkan info pelanggan: http: //localhost/index.php/rest/V1/customers/1 ) dengan curl, dan ia mengembalikan string JSON:
"{\" id \ ": 1, \" group_id \ ": 1, \" default_billing \ ": \" 1 \ ", \" Created_at \ ": \" 2016-12-13 14: 57: 30 \ " , \ "updated_at \": \ "2016-12-13 15:20:19 \", \ "Created_in \": \ "Tampilan Toko Default \", \ "email \": \ "75358050@qq.com \ ", \" firstname \ ": \" azol \ ", \" lastname \ ": \" young \ ", \" store_id \ ": 1, \" website_id \ ": 1, \" address \ ": [{ \ "id \": 1, \ "customer_id \": 1, \ "region \": {\ "region_code \": \ "AR \", \ "region \": \ "Arad \", \ "region_id \ ": 279}, \" region_id \ ": 279, \" country_id \ ": \" RO \ ", \" street \ ": [\" abc \ "], \" telephone \ ": \" 111 \ ",\"Kode Pos\":\"1111 \ ", \" city \ ": \" def \ ", \" firstname \ ": \" azol \ ", \" nama belakang \ ": \" young \ ", \" default_billing \ ": true}], \ "disable_auto_group_change \": 0} "
Responsnya adalah string JSON, tetapi semua kunci memiliki garis miring di dalamnya. Saya tahu saya bisa menghapus tebasan dengan str_replace
, tapi itu cara yang bodoh. Apakah ada cara lain untuk mengembalikan objek JSON tanpa garis miring di dalam kunci?
************ PEMBARUAN 2016.12.27 ************
Saya menempelkan kode pengujian saya di sini:
$method = 'GET';
$url = 'http://localhost/index.php/rest/V1/customers/1';
$data = [
'oauth_consumer_key' => $this::consumerKey,
'oauth_nonce' => md5(uniqid(rand(), true)),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_token' => $this::accessToken,
'oauth_version' => '1.0',
];
$data['oauth_signature'] = $this->sign($method, $url, $data, $this::consumerSecret, $this::accessTokenSecret);
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => [
'Authorization: OAuth ' . http_build_query($data, '', ','),
'Content-Type: application/json'
],
]);
$result = curl_exec($curl);
curl_close($curl);
// this code has slash still
//return stripslashes("hi i\" azol");
// has slashes still
//return stripcslashes("{\"id\":1,\"group_id\":1,\"default_billing\":\"1\",\"created_at\":\"2016-12-13 14:57:30\",\"updated_at\":\"2016-12-13 15:20:19\",\"created_in\":\"Default Store View\",\"email\":\"75358050@qq.com\",\"firstname\":\"azol\",\"lastname\":\"young\",\"store_id\":1,\"website_id\":1,\"addresses\":[{\"id\":1,\"customer_id\":1,\"region\":{\"region_code\":\"AR\",\"region\":\"Arad\",\"region_id\":279},\"region_id\":279,\"country_id\":\"RO\",\"street\":[\"abc\"],\"telephone\":\"111\",\"postcode\":\"1111\",\"city\":\"def\",\"firstname\":\"azol\",\"lastname\":\"young\",\"default_billing\":true}],\"disable_auto_group_change\":0}");
// has slashes still
//return json_encode(json_decode($result), JSON_UNESCAPED_SLASHES);
// this code will throw and expcetion:
// Undefined property: *****\*****\Model\Mycustom::$_response
//return $this->_response->representJson(json_encode($data));
return $result;
$json_string = stripslashes($result)
danreturn json_decode($json_string, true);
return json_encode($result, JSON_UNESCAPED_SLASHES);
?