PHP用CURL发送Content-type为application/json的POST请求方法

  • php curl发送json post
    复制代码
    function json_post($url, $data = NULL)
        {
    
            $curl = curl_init();
    
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            if(!$data){
                return 'data is null';
            }
            if(is_array($data))
            {
                $data = json_encode($data);
            }
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            curl_setopt($curl, CURLOPT_HEADER, 0);
            curl_setopt($curl, CURLOPT_HTTPHEADER,array(
                    'Content-Type: application/json; charset=utf-8',
                    'Content-Length:' . strlen($data),
                    'Cache-Control: no-cache',
                    'Pragma: no-cache'
            ));
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $res = curl_exec($curl);
            $errorno = curl_errno($curl);
            if ($errorno) {
                return $errorno;
            }
            curl_close($curl);
            return $res;
    
        }
    复制代码
  • PHP接受JSON POST
$data = json_decode(file_get_contents('php://input'), true);

 

 
posted @   爱写bug的程序员  阅读(736)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示