다른곳으로 JSON방식으로 전달해야한다는데요~~
본문
고수님들의 도움으로
<form ... /form> 양식으로 입력된 정보를
curl으로 다른 곳으로 보내는것은 확인하였습니다.
쩝 상대방이 json방식으로 데이타를 받는다는데~~
어떤것을 참고해봐야할까요???
부탁드립니다.
json 방식으로
<?php
$name = $_POST['name'] ;
$phone = $_POST['phone'] ;
$result= array('resut'=>true, 'member'=>array('name'=>$name));
echo json_encode($result); exit;
?>
한글은 깨지네요
{"resut":true,"member":{"name":"\ubc15\ub450\ub9ac","phone":"0130430"}}
curl 형태로 url 연동
==================================================================================
<?php
$ch = curl_init(); // Initiate cURL
$url = "http://xxxxxxx.com/xxx/xxx"; // Where you want to post data
$phone = $_POST['phone'];
$name = $_POST['name'] ;
$contents = $_POST['contents'] ;
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true); // Tell cURL you want to post something
curl_setopt($ch, CURLOPT_POSTFIELDS, "id=testd&pw=n1234&sendType=3&phone=$phone&name=$name&contents=$contents"); // Define what you want to post
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the output in string format
$output = curl_exec ($ch); // Execute
curl_close ($ch); // Close cURL handle
?>
!-->
답변 1
※ 참고사항 : http://php.net/manual/kr/function.json-encode.php
php 5.4이상
$array = array("foo","bar");
$result = json_encode($array,JSON_UNESCAPED_UNICODE);
php 5.3이하
function my_json_encode($arr)
{
//convmap since 0x80 char codes so it takes all multibyte codes (above ASCII 127). So such characters are being "hidden" from normal json_encoding
array_walk_recursive($arr, function (&$item, $key) { if (is_string($item)) $item = mb_encode_numericentity($item, array (0x80, 0xffff, 0, 0xffff), 'UTF-8'); });
return mb_decode_numericentity(json_encode($arr), array (0x80, 0xffff, 0, 0xffff), 'UTF-8');
}
$array = array("foo","bar");
$result = my_json_encode($array);
답변을 작성하시기 전에 로그인 해주세요.