首页 > php教程 > php手册 > 正文

如何POST一个JSON格式的数据给Restful服务,jsonrestful

WBOY
发布: 2016-06-13 08:54:33
原创
867 人浏览过

如何POST一个JSON格式的数据给Restful服务,jsonrestful

在Android/java平台上实现POST一个json数据:

<code><span class="typ">JSONObject<span class="pln"> jsonObj <span class="pun">=<span class="pln"> <span class="kwd">new<span class="pln"> <span class="typ">JSONObject<span class="pun">();<span class="pln">
jsonObj<span class="pun">.<span class="pln">put<span class="pun">(<span class="str">"username"<span class="pun">,<span class="pln"> username<span class="pun">);<span class="pln">
jsonObj<span class="pun">.<span class="pln">put<span class="pun">(<span class="str">"apikey"<span class="pun">,<span class="pln"> apikey<span class="pun">);<span class="pln">
<span class="com">// Create the POST object and add the parameters<span class="pln">
<span class="typ">HttpPost<span class="pln"> httpPost <span class="pun">=<span class="pln"> <span class="kwd">new<span class="pln"> <span class="typ">HttpPost<span class="pun">(<span class="pln">url<span class="pun">);<span class="pln">
<span class="typ">StringEntity<span class="pln"> entity <span class="pun">=<span class="pln"> <span class="kwd">new<span class="pln"> <span class="typ">StringEntity<span class="pun">(<span class="pln">jsonObj<span class="pun">.<span class="pln">toString<span class="pun">(),<span class="pln"> HTTP<span class="pun">.<span class="pln">UTF_8<span class="pun">);<span class="pln">
entity<span class="pun">.<span class="pln">setContentType<span class="pun">(<span class="str">"application/json"<span class="pun">);<span class="pln">
httpPost<span class="pun">.<span class="pln">setEntity<span class="pun">(<span class="pln">entity<span class="pun">);<span class="pln">
<span class="typ">HttpClient<span class="pln"> client <span class="pun">=<span class="pln"> <span class="kwd">new<span class="pln"> <span class="typ">DefaultHttpClient<span class="pun">();<span class="pln">
<span class="typ">HttpResponse<span class="pln"> response <span class="pun">=<span class="pln"> client<span class="pun">.<span class="pln">execute<span class="pun">(<span class="pln">httpPost<span class="pun">);</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
登录后复制

 

用curl可执行如下命令:

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

 

用jQuery:

 

<code><span class="pln">$<span class="pun">.<span class="pln">ajax<span class="pun">({<span class="pln">
  url<span class="pun">:<span class="pln">url<span class="pun">,<span class="pln">
  type<span class="pun">:<span class="str">"POST"<span class="pun">,<span class="pln">
  data<span class="pun">:<span class="pln">data<span class="pun">,<span class="pln">
  contentType<span class="pun">:<span class="str">"application/json; charset=utf-8"<span class="pun">,<span class="pln">
  dataType<span class="pun">:<span class="str">"json"<span class="pun">,<span class="pln">
  success<span class="pun">:<span class="pln"> <span class="kwd">function<span class="pun">(){<span class="pln">
    <span class="pun">...<span class="pln">
  <span class="pun">}<span class="pln">
<span class="pun">})</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
登录后复制

 

PHP用cUrl实现:

<span> 1</span> <span>$data</span> = <span>array</span>("name" => "Hagrid", "age" => "36"<span>); 
</span><span> 2</span> <span>$data_string</span> = json_encode(<span>$data</span><span>); 
</span><span> 3</span> <span>$ch</span> = curl_init('http://api.local/rest/users'<span>); 
</span><span> 4</span> curl_setopt(<span>$ch</span>, CURLOPT_CUSTOMREQUEST, "POST"<span>); 
</span><span> 5</span> curl_setopt(<span>$ch</span>, CURLOPT_POSTFIELDS, <span>$data_string</span><span>);
</span><span> 6</span> curl_setopt(<span>$ch</span>, CURLOPT_RETURNTRANSFER, <span>true</span><span>);
</span><span> 7</span> curl_setopt(<span>$ch</span>, CURLOPT_HTTPHEADER, <span>array</span><span>( 
</span><span> 8</span> 'Content-Type: application/json',
<span> 9</span> 'Content-Length: ' . <span>strlen</span>(<span>$data_string</span><span>)) 
</span><span>10</span> <span>); 
</span><span>11</span> <span>$result</span> = curl_exec(<span>$ch</span>);
登录后复制

 

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!