php通过curl post和get发送json数据实例 curl命令转为php源码
2016-04-24 10:33
901 查看
curl命令对应着php源码
12. curl命令工具测试 正常返回
获取状态:
curl -X GET -H "Content-Type:application/json" -H "Authorization: token 4e56266f2502936e0378ea6a985dc74a5bec4280" http://iot.espressif.cn/v1/datastreams/plug-status/datapoint/
返回{"status": 200, "datapoint": null}
设置状态:
curl -H "Authorization: token 6bcb3cdb69b07370f5ad73e7a856409802fdd735" -d "{\"datapoint\":{\"x\":1}}" http://iot.espressif.cn/v1/datastreams/plug-status/datapoint/?deliver_to_device=true
返回{"status": 404, "nonce": 333984364, "message": "remote device is disconnect"}
转为php发送curl OK
获取状态:
设置状态:
参考:
利用php curl发送json数据与curl post其它数据是一样的,下面我来给大家总结几个关于curl post发送json数据实例,希望能加深各位对curl post json数据的理解吧。小结,我们发现最核心的一句代码就是Content-Type:
application/json;这个是文件格式类型了。
例1
例2
例3
小黄人软件测试
curl测试
1.curl命令环境搭建 OK
http://blog.csdn.net/xukai871105/article/details/9323761
curl环境测试
curl www.baidu.com
返回
C:\Users\xhrrj>curl www.baidu.com
<!DOCTYPE html><!--STATUS OK--><html>后面略
说明环境成功
2.espressif.cn测试 OK
获取当前 key
curl -H "Authorization: token 6396199953e8224f24594a8c2407a96e79633f0a" https://iot.espressif.cn/v1/key/
pause
返回
{"status": 200, "key": {"updated": "2016-04-14 16:50:23", "user_id": 6366, "name": "device key", "created": "2016-04-14 16:50:23", "source_ip": "*", "level": 0, "is_master_key": 1, "visibly": 1, "datastream_id": 0, "datastream_tmpl_id": 0, "token": "6396199953e8224f24594a8c2407a96e79633f0a",
"activate_status": 1, "is_owner_key": 0, "access_methods": "*", "authorized_user_id": 0, "scope": 3, "expired_at": "2290-01-28 16:50:23", "product_id": 0, "id": 151576, "device_id": 129793}}
3.列出产品 OK
发送
curl -H "Authorization: token 97db4f9d77c23e99204668ef5dffd6177aa926a7" https://iot.espressif.cn/v1/products/ >F:/WWW/out.txt
pause
返回
{"status": 200, "products": [{"status": 1, "updated": "2016-04-14 16:44:21", "description": "", "creator_name": "chenhao", "created": "2016-04-14 16:44:21", "rom_version": "", "visibly": 1, "ptype": 23701, "activated_devices_count": 0, "creator_user_id": 6366,
"serial": "f18e7250", "devices_count": 1, "id": 2604, "is_private": 1, "name": "switch"}, {"status": 1, "updated": "2016-04-14 16:50:23", "description": "", "creator_name": "chenhao", "created": "2016-04-14 16:50:23", "rom_version": "", "visibly": 1, "ptype":
34100, "activated_devices_count": 0, "creator_user_id": 6366, "serial": "16dfa58f", "devices_count": 1, "id": 2605, "is_private": 1, "name": "computer"}]}
换成php 确只返回200 没有数据
<?php
$header= array('Authorization: token 97db4f9d77c23e99204668ef5dffd6177aa926a7');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, 'https://iot.espressif.cn/v1/products/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$response = curl_exec($ch);
echo $response;
print_r($response);
$json_parameter =$response;
$array = json_decode($json_parameter);
echo $array;
curl_close($ch);
?>
4.注册 error
curl -d '{"username":"chenhao0569","email":"1287598558@qq.com","password":"*****"}' https://iot.espressif.cn/v1/user/join/
pause
返回
<h1>Server Error (500)</h1>
5.注册验证 error
发送
F:\WWW>curl https://iot.espressif.cn/v1/user/join/validate/:token/
<h1>Not Found</h1><p>The requested URL /v1/user/join/validate/:token/ was not found on this server.</p>
F:\WWW>pause
请按任意键继续. . .
6.登录测试 error
F:\WWW>curl -d '{"email":"345139427@qq.com","password":"******","remember":1}' https://iot.espressif.cn/v1/user/login/
{"status": 400, "message": "400 Bad Request", "result": "failed"}
F:\WWW>pause
请按任意键继续. . .
7.重置密码 error
F:\WWW>curl -d '{"email":"345139427@qq.com"}' https://iot.espressif.cn/v1/user/resetpassword/mail/
{"status": 404, "message": "can not find that email"}
F:\WWW>pause
请按任意键继续. . .
8.传递一个数据点到设备 OK
F:\WWW>curl -H "Authorization: token b1da2cfd24231d2a463a4a36267b97e7d504f21a" -d '{"datapoint":{"x":1}}' http://iot.espressif.cn/v1/datastreams/switch-status/datapoint/?deliver_to_device=true
{"status": 404, "nonce": 44688077, "message": "remote device is disconnect"}
F:\WWW>pause
请按任意键继续. . .
9.推送一个数据点到服务器端 服务器收到了。但返回的是500
F:\WWW>curl -H "Authorization: token cd7a992279c018b6079ee01957603b28a8223927" -d '{"datapoint":{"x":1}}' http://iot.espressif.cn/v1/datastreams/CPU/datapoint/
<h1>Server Error (500)</h1>
F:\WWW>pause
请按任意键继续. . .
10.php curl测试 登录测试 返回200 但无cookie?????
<?php
$post_data = array ("email" => "345139427@qq.com","password" => "chenhao+0568","remember" => "1");
$post_data="'email=345139427@qq.com & password=chenhao+0568 & remember=1"; //提交内容
$url = "https://iot.espressif.cn/v1/user/login/"; //地址
$ch = curl_init();//新建curl
curl_setopt($ch, CURLOPT_URL, $url);//url
curl_setopt($ch, CURLOPT_POST, true); //post
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//post内容
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
echo curl_exec($ch); //输出
curl_close($ch);
?>
不行
<?php
$headr = array();
$headr[] = 'Authorization: token cd7a992279c018b6079ee01957603b28a8223927';
//$post_data = array ("email" => "345139427@qq.com","password" => "chenhao+0568","remember" => "1");
$post_data="datapoint={"x":1}"; //提交内容
$url = "http://iot.espressif.cn/v1/datastreams/CPU/datapoint/"; //地址
$ch = curl_init();//新建curl
curl_setopt($ch, CURLOPT_URL, $url);//url
curl_setopt($crl, CURLOPT_HTTPHEADER,$headr);
curl_setopt($ch, CURLOPT_POST, true); //post
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//post内容
echo curl_exec($ch); //输出
curl_close($ch);
?>
不行
<?php
echo "Hello";
$header= array('Authorization: token 97db4f9d77c23e99204668ef5dffd6177aa926a7');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, 'https://iot.espressif.cn/v1/products/');
curl_setopt($ch, CURLOPT_POST, true); //post
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$response = curl_exec($ch);
echo $response;
print_r($response);
$json_parameter =$response;
$array = json_decode($json_parameter);
echo $array;
curl_close($ch);
file_put_contents("filename.txt","接收到数据:"+$response+"array"+$array); //存"" 即清空
?>
10.curl post发送与接收1.0OK
<?php
$url = "http://localhost/web_services.php";
$post_data = array ("username" => "bob","key" => "12345");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// post数据
curl_setopt($ch, CURLOPT_POST, 1);
// post的变量
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
//打印获得的数据
print_r($output);
?>
<html>
<body>
Welcome <?php echo $_POST["username"]; ?>.<br />
You are <?php echo $_POST["key"]; ?> years old.
</body>
</html>
11.get 测试OK
<?php
//curl -X GET -H "Content-Type:application/json" -H "Authorization: token 4e56266f2502936e0378ea6a985dc74a5bec4280" http://iot.espressif.cn/v1/datastreams/plug-status/datapoint/
$url = "http://localhost/web_services.php";
$post_data = array ("username" => "bob","key" => "12345");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
//打印获得的数据
print_r($output);
?>
12. curl命令工具测试 正常返回
获取状态:
curl -X GET -H "Content-Type:application/json" -H "Authorization: token 4e56266f2502936e0378ea6a985dc74a5bec4280" http://iot.espressif.cn/v1/datastreams/plug-status/datapoint/
返回{"status": 200, "datapoint": null}
设置状态:
curl -H "Authorization: token 6bcb3cdb69b07370f5ad73e7a856409802fdd735" -d "{\"datapoint\":{\"x\":1}}" http://iot.espressif.cn/v1/datastreams/plug-status/datapoint/?deliver_to_device=true
返回{"status": 404, "nonce": 333984364, "message": "remote device is disconnect"}
转为php发送curl OK
获取状态:
<?php //curl命令 curl -X GET -H "Content-Type:application/json" -H "Authorization: token 4e56266f2502936e0378ea6a985dc74a5bec4280" http://iot.espressif.cn/v1/datastreams/plug-status/datapoint/ $url = "http://iot.espressif.cn/v1/datastreams/plug-status/datapoint/"; $header= array( 'Content-Type: application/json', 'Authorization: token 4e56266f2502936e0378ea6a985dc74a5bec4280'); $ch = curl_init(); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); //打印获得的数据 print_r($output); ?>
设置状态:
<?php //curl命令 curl -H "Authorization: token 6bcb3cdb69b07370f5ad73e7a856409802fdd735" -d "{\"datapoint\":{\"x\":1}}" http://iot.espressif.cn/v1/datastreams/plug-status/datapoint/?deliver_to_device=true $url = "http://iot.espressif.cn/v1/datastreams/plug-status/datapoint/?deliver_to_device=true"; $header= array( 'Content-Type: application/json', 'Authorization: token 6bcb3cdb69b07370f5ad73e7a856409802fdd735'); $data=' { "datapoint":[ { "x":"1", }] }'; $ch = curl_init(); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); //打印获得的数据 print_r($output); ?>
参考:
利用php curl发送json数据与curl post其它数据是一样的,下面我来给大家总结几个关于curl post发送json数据实例,希望能加深各位对curl post json数据的理解吧。小结,我们发现最核心的一句代码就是Content-Type:
application/json;这个是文件格式类型了。
例1
$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);
例2
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); } $url = "http://xx.xx.cn"; $data = json_encode(array('a'=>1, 'b'=>2)); list($return_code, $return_content) = http_post_data($url, $data);
例3
$data=' { "button":[ { "type":"click", "name":"今日歌曲", "key":"V1001_TODAY_MUSIC" }, { "type":"click", "name":"歌手简介", "key":"V1001_TODAY_SINGER" }, { "name":"菜单", "sub_button":[ { "type":"click", "name":"hello word", "key":"V1001_HELLO_WORLD" }, { "type":"click", "name":"赞一下我们", "key":"V1001_GOOD" }] }] }'; $ch = curl_init($urlcon); //请求的URL地址 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//$data JSON类型字符串 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data))); $data = curl_exec($ch); print_r($data);//创建成功返回:{"errcode":0,"errmsg":"ok"}
小黄人软件测试
curl测试
1.curl命令环境搭建 OK
http://blog.csdn.net/xukai871105/article/details/9323761
curl环境测试
curl www.baidu.com
返回
C:\Users\xhrrj>curl www.baidu.com
<!DOCTYPE html><!--STATUS OK--><html>后面略
说明环境成功
2.espressif.cn测试 OK
获取当前 key
curl -H "Authorization: token 6396199953e8224f24594a8c2407a96e79633f0a" https://iot.espressif.cn/v1/key/
pause
返回
{"status": 200, "key": {"updated": "2016-04-14 16:50:23", "user_id": 6366, "name": "device key", "created": "2016-04-14 16:50:23", "source_ip": "*", "level": 0, "is_master_key": 1, "visibly": 1, "datastream_id": 0, "datastream_tmpl_id": 0, "token": "6396199953e8224f24594a8c2407a96e79633f0a",
"activate_status": 1, "is_owner_key": 0, "access_methods": "*", "authorized_user_id": 0, "scope": 3, "expired_at": "2290-01-28 16:50:23", "product_id": 0, "id": 151576, "device_id": 129793}}
3.列出产品 OK
发送
curl -H "Authorization: token 97db4f9d77c23e99204668ef5dffd6177aa926a7" https://iot.espressif.cn/v1/products/ >F:/WWW/out.txt
pause
返回
{"status": 200, "products": [{"status": 1, "updated": "2016-04-14 16:44:21", "description": "", "creator_name": "chenhao", "created": "2016-04-14 16:44:21", "rom_version": "", "visibly": 1, "ptype": 23701, "activated_devices_count": 0, "creator_user_id": 6366,
"serial": "f18e7250", "devices_count": 1, "id": 2604, "is_private": 1, "name": "switch"}, {"status": 1, "updated": "2016-04-14 16:50:23", "description": "", "creator_name": "chenhao", "created": "2016-04-14 16:50:23", "rom_version": "", "visibly": 1, "ptype":
34100, "activated_devices_count": 0, "creator_user_id": 6366, "serial": "16dfa58f", "devices_count": 1, "id": 2605, "is_private": 1, "name": "computer"}]}
换成php 确只返回200 没有数据
<?php
$header= array('Authorization: token 97db4f9d77c23e99204668ef5dffd6177aa926a7');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, 'https://iot.espressif.cn/v1/products/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$response = curl_exec($ch);
echo $response;
print_r($response);
$json_parameter =$response;
$array = json_decode($json_parameter);
echo $array;
curl_close($ch);
?>
4.注册 error
curl -d '{"username":"chenhao0569","email":"1287598558@qq.com","password":"*****"}' https://iot.espressif.cn/v1/user/join/
pause
返回
<h1>Server Error (500)</h1>
5.注册验证 error
发送
F:\WWW>curl https://iot.espressif.cn/v1/user/join/validate/:token/
<h1>Not Found</h1><p>The requested URL /v1/user/join/validate/:token/ was not found on this server.</p>
F:\WWW>pause
请按任意键继续. . .
6.登录测试 error
F:\WWW>curl -d '{"email":"345139427@qq.com","password":"******","remember":1}' https://iot.espressif.cn/v1/user/login/
{"status": 400, "message": "400 Bad Request", "result": "failed"}
F:\WWW>pause
请按任意键继续. . .
7.重置密码 error
F:\WWW>curl -d '{"email":"345139427@qq.com"}' https://iot.espressif.cn/v1/user/resetpassword/mail/
{"status": 404, "message": "can not find that email"}
F:\WWW>pause
请按任意键继续. . .
8.传递一个数据点到设备 OK
F:\WWW>curl -H "Authorization: token b1da2cfd24231d2a463a4a36267b97e7d504f21a" -d '{"datapoint":{"x":1}}' http://iot.espressif.cn/v1/datastreams/switch-status/datapoint/?deliver_to_device=true
{"status": 404, "nonce": 44688077, "message": "remote device is disconnect"}
F:\WWW>pause
请按任意键继续. . .
9.推送一个数据点到服务器端 服务器收到了。但返回的是500
F:\WWW>curl -H "Authorization: token cd7a992279c018b6079ee01957603b28a8223927" -d '{"datapoint":{"x":1}}' http://iot.espressif.cn/v1/datastreams/CPU/datapoint/
<h1>Server Error (500)</h1>
F:\WWW>pause
请按任意键继续. . .
10.php curl测试 登录测试 返回200 但无cookie?????
<?php
$post_data = array ("email" => "345139427@qq.com","password" => "chenhao+0568","remember" => "1");
$post_data="'email=345139427@qq.com & password=chenhao+0568 & remember=1"; //提交内容
$url = "https://iot.espressif.cn/v1/user/login/"; //地址
$ch = curl_init();//新建curl
curl_setopt($ch, CURLOPT_URL, $url);//url
curl_setopt($ch, CURLOPT_POST, true); //post
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//post内容
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
echo curl_exec($ch); //输出
curl_close($ch);
?>
不行
<?php
$headr = array();
$headr[] = 'Authorization: token cd7a992279c018b6079ee01957603b28a8223927';
//$post_data = array ("email" => "345139427@qq.com","password" => "chenhao+0568","remember" => "1");
$post_data="datapoint={"x":1}"; //提交内容
$url = "http://iot.espressif.cn/v1/datastreams/CPU/datapoint/"; //地址
$ch = curl_init();//新建curl
curl_setopt($ch, CURLOPT_URL, $url);//url
curl_setopt($crl, CURLOPT_HTTPHEADER,$headr);
curl_setopt($ch, CURLOPT_POST, true); //post
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//post内容
echo curl_exec($ch); //输出
curl_close($ch);
?>
不行
<?php
echo "Hello";
$header= array('Authorization: token 97db4f9d77c23e99204668ef5dffd6177aa926a7');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, 'https://iot.espressif.cn/v1/products/');
curl_setopt($ch, CURLOPT_POST, true); //post
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$response = curl_exec($ch);
echo $response;
print_r($response);
$json_parameter =$response;
$array = json_decode($json_parameter);
echo $array;
curl_close($ch);
file_put_contents("filename.txt","接收到数据:"+$response+"array"+$array); //存"" 即清空
?>
10.curl post发送与接收1.0OK
<?php
$url = "http://localhost/web_services.php";
$post_data = array ("username" => "bob","key" => "12345");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// post数据
curl_setopt($ch, CURLOPT_POST, 1);
// post的变量
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
//打印获得的数据
print_r($output);
?>
<html>
<body>
Welcome <?php echo $_POST["username"]; ?>.<br />
You are <?php echo $_POST["key"]; ?> years old.
</body>
</html>
11.get 测试OK
<?php
//curl -X GET -H "Content-Type:application/json" -H "Authorization: token 4e56266f2502936e0378ea6a985dc74a5bec4280" http://iot.espressif.cn/v1/datastreams/plug-status/datapoint/
$url = "http://localhost/web_services.php";
$post_data = array ("username" => "bob","key" => "12345");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
//打印获得的数据
print_r($output);
?>
相关文章推荐
- php设计模式之装饰模式
- php数组原样输出,带下标
- getpid()函数
- Laravel的三种安装方法总结
- 夺命雷公狗ThinkPHP项目之----企业网站11之栏目的删除完成
- 夺命雷公狗ThinkPHP项目之----企业网站10之栏目的编辑完善(无限极分类的完成)
- PHP7之匿名类
- PHPstorm创建注释模版
- PHP安装xdebug调试
- php 注释
- 完美扫描PHP特殊一句话后门
- Dubbo部署报创建productionModeSensiblePostProcessor异常处理
- yii2集成富文本编辑器redactor
- Laravel5学生成绩管理系统-02-Blade模板
- Laravel5学生成绩管理系统-03-登陆登出功能-权限-中间件
- PHP学习资料分享
- Ubuntu typing 'php' in terminal shows a lot of errors 终端terminal 输入php 错误
- phpwind < v6 版本命令执行漏洞
- thinkphp自带验证码出错
- PHP简单分页的实现