如果是GET的话就不必那么多设置。但是基本需要用到POST就需要用到以下的几个设置选项。

<?php 
$username = "admin";
$password = "123467";
$urlpost = "username={$username}&password={$password}";
$curl = curl_init();//初始化会话
curl_setopt($curl,CURLOPT_URL,"http://localhost/login");
curl_setopt($curl,CURLOPT_RETURNTRANSFER,0);//post当中是不可见的,所以设置为0
curl_setopt($curl,CURLOPT_POST,1);//开启post
curl_setopt($curl,CURLOPT_POSTFIELDS,$urlpost);//使用POST来操作要发送的文件
$data = curl_exec($curl);//执行
curl_clsoe($curl);//关闭
 ?>

 

如果说是POST的传输那么以下这几种选项是必须要设置的:

curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$postdata);
curl_setopt($curl,CURLOPT_HTTPHEADER,array("application/x-wwww-form-urlencode;cahrset=utf-8","content-length:".strlen($postdata)));

那么我们来写一个使用curl来进行传输的案例:

1.php

<?php 
$data = "username=admin&password=123456";
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,"http://127.0.0.1/login.php");
//returnTransfer即为是否输出到显示页面,0为输出到显示页面,1为不输出到显示页面。
curl_setopt($curl,CURLOPT_RETURNTRANSFER,0);
//需要用到POST所以POST这个一定要开启状态,即为1.
curl_setopt($curl,CURLOPT_POST,1);
//使用POSTFIELDS来接收$data的数据
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
//执行$curl
curl_exec($curl);
//关闭$curl
curl_close($curl);
 ?>

login.php

<?php 
if(isset($_POST['username']) and isset($_POST['password'])){
    if($_POST['username'] == 'admin' && $_POST['password'] == '123456'){
    echo "<script>alert('成功登陆')</script>";
}else{
    echo "<script>alert('登陆失败')</script>";
}
}
 ?>

如果登陆成功则会弹出“成功登陆”否则会弹出“登陆失败”。

 

相关文章:

  • 2021-12-26
  • 2021-06-17
  • 2021-11-02
  • 2022-12-23
  • 2022-02-07
  • 2021-10-07
  • 2021-06-03
  • 2022-01-10
猜你喜欢
  • 2022-01-20
  • 2022-01-11
  • 2021-07-08
  • 2021-07-20
  • 2022-01-11
  • 2021-12-19
  • 2021-12-25
相关资源
相似解决方案