zoukankan      html  css  js  c++  java
  • 跟着百度学PHP[17]-PHP扩展CURL的POST传输数据

    如果是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>";
    }
    }
     ?>

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

  • 相关阅读:
    hadoop集群部署入门(传智Hadoop学习)
    遇到问题了!
    MD5和TreeView的学习
    今天晚上完成了一个登录功能
    第一次来到博客园!
    单元测试--测?
    单元测试-公司实习1
    Mariadb数据库小结
    [奋斗的人生] 学习,总结,感恩,回馈
    将博客搬至CSDN
  • 原文地址:https://www.cnblogs.com/xishaonian/p/6525278.html
Copyright © 2011-2022 走看看