php通过curl post和get发送json数据实例 curl命令转为php源码

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

$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
后面略
说明环境成功




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
返回

Server Error (500)




5.注册验证 error
发送
F:\WWW>curl https://iot.espressif.cn/v1/user/join/validate/:token/

Not Found

The requested URL /v1/user/join/validate/:token/ was not found on this server.


F:\WWW>pause
请按任意键继续. . .


6.登录测试 error
F:\WWW>curl -d '{"email":"[email protected]","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":"[email protected]"}' 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/

Server Error (500)


F:\WWW>pause
请按任意键继续. . .


10.php curl测试  登录测试 返回200 但无cookie?????
$post_data = array ("email" => "[email protected]","password" => "chenhao+0568","remember" => "1");
$post_data="'[email protected] & 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); 
?>






不行
$headr = array();
$headr[] = 'Authorization: token cd7a992279c018b6079ee01957603b28a8223927';
//$post_data = array ("email" => "[email protected]","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); 
?>




不行
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
    $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);
?>




Welcome .

You are years old.






11.get 测试OK




//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);
?>











你可能感兴趣的:(创新与突破)