curl命令对应着php源码
12. curl命令工具测试 正常返回
获取状态:
参考:
例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);
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);
$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
后面略
说明环境成功
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 没有数据
$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":"[email protected]","password":"*****"}' https://iot.espressif.cn/v1/user/join/
pause
返回
The requested URL /v1/user/join/validate/:token/ was not found on this server.