JSON

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

字号+ 作者:H5之家 来源:H5之家 2015-10-21 11:10 我要评论( )

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

   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  = "";
$data = json_encode(array('a'=>1, 'b'=>2));

list($return_code, $return_content) = http_post_data($url, $data);

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • php CI 实战教程:[5]用curl获取json并解析

    php CI 实战教程:[5]用curl获取json并解析

    2016-02-26 17:00

  • AFN post JSON

    AFN post JSON

    2016-01-21 18:13

  • Http连接如何实现GET POST JSON数据与下载图片显示进度

    Http连接如何实现GET POST JSON数据与下载图片显示进度

    2016-01-17 18:19

  • http,json,curl之间关系

    http,json,curl之间关系

    2016-01-17 14:03

网友点评