当前位置:首页 > PHP教程 > php高级应用 > 列表

php 通过curl post发送json数据实例

发布:smiling 来源: PHP粉丝网  添加日期:2014-01-10 14:00:47 浏览: 评论:0 

利用php curl发送json数据与curl post其它数据是一样的,下面我来给大家总结几个关于curl post发送json数据实例,希望能加深各位对curl post json数据的理解吧。

例1代码如下:

  1. $data = array("name" => "Hagrid""age" => "36");                                                                   
  2. $data_string = json_encode($data);                                                                                   
  3.  
  4. $ch = curl_init('http://api.local/rest/users');                                                                     
  5. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
  6. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                 
  7. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                     
  8. curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                         
  9.     'Content-Type: application/json',                                                                               
  10.     'Content-Length: ' . strlen($data_string))                                                                       
  11. );                                                                                                                   
  12.  
  13. $result = curl_exec($ch); 

例2,代码如下:

  1. function http_post_data($url$data_string) { 
  2.         $ch = curl_init(); 
  3.         curl_setopt($ch, CURLOPT_POST, 1); 
  4.         curl_setopt($ch, CURLOPT_URL, $url); 
  5.         curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 
  6.         curl_setopt($ch, CURLOPT_HTTPHEADER, array
  7.             'Content-Type: application/json; charset=utf-8'
  8.             'Content-Length: ' . strlen($data_string)) 
  9.         ); 
  10.         ob_start(); 
  11.         curl_exec($ch); 
  12.         $return_content = ob_get_contents(); 
  13.         ob_end_clean(); 
  14.         $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
  15.         return array($return_code$return_content); 
  16.     } 
  17. $url  = "http://xx.xx.cn"
  18. $data = json_encode(array('a'=>1, 'b'=>2)); 
  19. list($return_code$return_content) = http_post_data($url$data); 

例3代码如下:

  1. $data=' { 
  2.      "button":[ 
  3.      {     
  4.           "type":"click"
  5.           "name":"今日歌曲"
  6.           "key":"V1001_TODAY_MUSIC" 
  7.       }, 
  8.       { 
  9.            "type":"click"
  10.            "name":"歌手简介"
  11.            "key":"V1001_TODAY_SINGER" 
  12.       }, 
  13.       { 
  14.            "name":"菜单"
  15.            "sub_button":[ 
  16.             { 
  17.                "type":"click"
  18.                "name":"hello word"
  19.                "key":"V1001_HELLO_WORLD" 
  20.             }, 
  21.             { 
  22.                "type":"click"
  23.                "name":"赞一下我们"
  24.                "key":"V1001_GOOD" 
  25.             }] 
  26.        }] 
  27.  }'; 
  28. $ch = curl_init($urlcon); //请求的URL地址 
  29. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
  30. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//$data JSON类型字符串 
  31. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  32. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json''Content-Length: ' . strlen($data))); 
  33. $data = curl_exec($ch); 
  34. print_r($data);//创建成功返回:{"errcode":0,"errmsg":"ok"} 

小结,我们发现最核心的一句代码就是Content-Type: application/json;这个是文件格式类型了.

Tags: php curl post json数据 实例

分享到: