您好,欢迎访问一九零五行业门户网

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

利用php curl发送json数据与curl post其它数据是一样的,下面我来给大家总结几个关于curl post发送json数据实例,希望能加深各位对curl post 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}
小结,我们发现最核心的一句代码就是content-type: application/json;这个是文件格式类型了。
http://www.bkjia.com/phpjc/633132.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/633132.htmltecharticle利用php curl发送json数据与curl post其它数据是一样的,下面我来给大家总结几个关于curl post发送json数据实例,希望能加深各位对curl post json数...
其它类似信息

推荐信息