这篇文章主要介绍了PHP基于curl模拟post提交json数据操作,结合实例形式分析了php使用curl实现post方式提交json数据相关操作步骤与注意事项,代码简单实用,需要的朋友可以参考下
本文实例讲述了PHP基于curl模拟post提交json数据。分享给大家供大家参考,具体如下:
这里php模拟post提交json数据操作的关键是在头部设置Content-Type
<?php
header("Content-type:application/json;charset=utf-8");
$url="http://192.168.10.234:8080/uc/login/loginid";
$param=array(
//注册字段
"name"=>"test001",
"pass"=>"xxxx",
);
$data = json_encode($param);
list($return_code, $return_content) = http_post_data($url, $data);//return_code是http状态码
print_r($return_content);exit;
function http_post_data($url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json; charset=utf-8",
"Content-Length: " . strlen($data_string))
);
ob_start();
curl_exec($ch);
$return_content = ob_get_contents();
ob_end_clean();
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return array($return_code, $return_content);
}
PS:这里再为大家推荐几款比较实用的json在线工具供大家参考使用:
在线JSON代码检验、检验、美化、格式化工具:
http://tools.xujingzi.com/code/json
JSON在线格式化工具:
http://tools.xujingzi.com/code/jsonformat
在线XML/JSON互相转换工具:
http://tools.xujingzi.com/code/xmljson
json代码在线格式化/美化/压缩/编辑/转换工具:
http://tools.xujingzi.com/code/jsoncodeformat
C语言风格/HTML/CSS/json代码格式化美化工具:
http://tools.xujingzi.com/code/ccode_html_css_json
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php curl用法总结》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP数据结构与算法教程》及《PHP中json格式数据操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
本文标题为:PHP基于curl模拟post提交json数据示例


基础教程推荐
- thinkPHP3.2.2框架行为扩展及demo示例 2022-11-07
- php中使用array_filter()函数过滤数组实例讲解 2023-05-19
- laravel model模型定义实现开启自动管理时间created_at,updated_at 2023-03-02
- TP5 连接多个数据库及使用方法 2023-08-30
- PHP实现生成数据字典功能示例 2022-10-18
- PHP实现创建一个RPC服务操作示例 2023-04-01
- TP5(thinkPHP5框架)基于bootstrap实现的单图上传插件用法示例 2023-01-19
- PHP删除数组中指定值的元素常用方法实例分析【4种方法】 2022-11-12
- PHP使用SMTP邮件服务器发送邮件示例 2022-11-16
- PHP数据加密方式梳理介绍 2023-07-03