国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

POST一個JSON格式的數據給Restful服務實例詳解

2024-05-04 23:56:36
字體:
來源:轉載
供稿:網友

在Android/Java平臺上實現POST一個json數據:

JSONObject jsonObj = new JSONObject();jsonObj.put("username", username);jsonObj.put("apikey", apikey);// Create the POST object and add the parametersHttpPost httpPost = new HttpPost(url);StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);entity.setContentType("application/json");httpPost.setEntity(entity);HttpClient client = new DefaultHttpClient();HttpResponse response = client.execute(httpPost);

用curl可執行如下命令:

curl -l -H "Content-type: application/json" -X POST -d '{"phone":"13521389587","password":"test"}' http://domain/apis/users.json

用jQuery:

$.ajax({ url:url, type:"POST", data:data, contentType:"application/json; charset=utf-8", dataType:"json", success: function(){  ... }})

PHP用cUrl實現:

$data = array("name" => "Hagrid", "age" => "36");                                   $data_string = json_encode($data);    $ch = curl_init('http://api.local/rest/users');    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); 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

 

注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 鄂托克前旗| 邢台县| 常山县| 井研县| 扶沟县| 乌恰县| 屏山县| 洪泽县| 万山特区| 闽清县| 阳朔县| 邯郸县| 水富县| 合江县| 阿坝| 交城县| 潞城市| 铁力市| 翁源县| 堆龙德庆县| 平塘县| 安岳县| 灵石县| 鹤庆县| 崇左市| 松桃| 叶城县| 永定县| 绥江县| 安乡县| 铁岭县| 广南县| 永胜县| 象山县| 沙洋县| 荔波县| 友谊县| 合阳县| 南投市| 崇仁县| 襄樊市|