美文网首页
PHP用CURL发送Content-type为applicati

PHP用CURL发送Content-type为applicati

作者: Mracale | 来源:发表于2024-06-11 09:25 被阅读0次

    一、PHP发送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);
    

    相关文章

      网友评论

          本文标题:PHP用CURL发送Content-type为applicati

          本文链接:https://www.haomeiwen.com/subject/ouvwqjtx.html