Rumah > pembangunan bahagian belakang > tutorial php > 如何POST一个JSON格式的数据给Restful服务

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

WBOY
Lepaskan: 2016-07-29 09:15:39
asal
871 orang telah melayarinya

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

<code><span>JSONObject<span> jsonObj <span>=<span><span>new<span><span>JSONObject<span>();<span>jsonObj<span>.<span>put<span>(<span>"username"<span>,<span> username<span>);<span>jsonObj<span>.<span>put<span>(<span>"apikey"<span>,<span> apikey<span>);<span><span>// Create the POST object and add the parameters<span><span>HttpPost<span> httpPost <span>=<span><span>new<span><span>HttpPost<span>(<span>url<span>);<span><span>StringEntity<span> entity <span>=<span><span>new<span><span>StringEntity<span>(<span>jsonObj<span>.<span>toString<span>(),<span> HTTP<span>.<span>UTF_8<span>);<span>entity<span>.<span>setContentType<span>(<span>"application/json"<span>);<span>httpPost<span>.<span>setEntity<span>(<span>entity<span>);<span><span>HttpClient<span> client <span>=<span><span>new<span><span>DefaultHttpClient<span>();<span><span>HttpResponse<span> response <span>=<span> client<span>.<span>execute<span>(<span>httpPost<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></span></code>
Salin selepas log masuk

用curl可执行如下命令:

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

用jQuery:

<code><span>$<span>.<span>ajax<span>({<span>  url<span>:<span>url<span>,<span>  type<span>:<span>"POST"<span>,<span>  data<span>:<span>data<span>,<span>  contentType<span>:<span>"application/json; charset=utf-8"<span>,<span>  dataType<span>:<span>"json"<span>,<span>  success<span>:<span><span>function<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>
Salin selepas log masuk

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>);
Salin selepas log masuk

以上就介绍了如何POST一个JSON格式的数据给Restful服务,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan