PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > PHP教程 > 正文

    php通过http post发送json数据

    作者:w634381967来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:今天项目中写的代码php通过http post发送json数据,分享给大家,希望对你有帮助。
    php通过http 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  = "http://xx.xx.cn";
    $data = json_encode(array('a'=>1, 'b'=>2)); 

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

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-10-1067-1.html
    相关热词搜索: