**一.curl资源迁移curl对象** #php8开始curl资源也无缝迁移到curl对象 **二.查看类型** ~~~ $curl = curl_init(); ~~~ **三.curl获取数据** 1.get.php ~~~ $arr=[ 'user1'=>'123', 'user2'=>'456', 'user3'=>'789', ]; echo json_encode($arr); ~~~ 2.index.php ~~~ $url='http://bbs.linux.com/get.php'; //生成curl对象 $curl = curl_init(); //设置url curl_setopt($curl, CURLOPT_URL, $url); //curl_exec是返回获取值 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //获取并返回 echo curl_exec($curl); ~~~ **四.hosts域名解析** #/etc/hosts ~~~ 127.0.0.1 bbs.linux.com ~~~ **五.curl发送post数据** 1.get.php #直接返回curl过来的post数据 ~~~ echo json_encode($_POST); ~~~ #模拟登录 ~~~ if($_POST['user1']=='222'){ echo '{"login":"yes"}'; }else{ echo '{"login":"no"}'; } ~~~ 2.index.php ~~~ $url='http://bbs.linux.com/get.php'; $curlPost=[ 'user1'=>'222', 'user2'=>'555', 'user3'=>'999', ]; //生成curl对象 $curl = curl_init(); //设置url curl_setopt($curl, CURLOPT_URL, $url); //curl_exec是返回获取值 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //设置post请求 curl_setopt($curl, CURLOPT_POST, 1); //发送post数据 curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost); //获取并返回 echo curl_exec($curl); ~~~