curl几个步骤

一:curl的基本操作

<?php
    //1.初始化curl,返回资源
    $curl = curl_init();

    //2.设置curl工具请求的服务器文件地址
    //参数1:curl资源
    //参数2:设置请求的选项
    //参数3:请求选项的值
    curl_setopt($curl,CURLOPT_URL,'http://www/baidu.com/index.php');

    //3.发出请求
    curl_exec($curl);
    
    //4.关闭curl
    curl_close($curl);

 

二:curl模拟post提交的方式

<?php
    //1.初始化curl,返回资源
    $ch = curl_init();

    //2.设置选项,设置请求的服务器地址
    //开启post提交
    curl_setopt($ch,CURLOPT_POST,ture);

    //设置提交什么数据过去
    $data = array('order'=>'ESC1000000012');
    curl_setopt($ch,CURLOPT_POSTFIELDS,$value);

    //设置请求的地址
    curl_setopt($ch,CURLOPT_URL,'http://localhost/3.post.handl.php');
    
    //3.发出请求
    curl_exec($ch);

    //4.关闭资源
    curl_close($ch);

 

三:curl的一些常用选项

(1)设置将结果返回而不是直接显示

CURLOPT_RETURNTRANSFER

<?php
    //1.初始化curl,返回资源
    $curl = curl_init();

    //2.设置curl工具请求的服务器文件地址
    //参数1:curl资源
    //参数2:设置请求的选项
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);//保存结果,而不展示
    curl_setopt($curl,CURLOPT_URL,'http://www/baidu.com/index.php');

    //3.发出请求
    $res = curl_exec($curl);
    var_dump($res);
    //4.关闭curl
    curl_close($curl);

(2)针对https的请求,需要验证客户端的安全证书,一般设跳过验证

CURL_SSL_VERIFYHOST,CURL_SSL_VERIFYPEER

<?php
    //1.初始化curl,返回资源
    $ch = curl_init();

    //2.设置选项,设置请求的服务器地址
    //开启post提交
    curl_setopt($ch,CURLOPT_POST,ture);

    //设置提交什么数据过去
    $data = array('order'=>'ESC1000000012');
    curl_setopt($ch,CURLOPT_POSTFIELDS,$value);

    //设置请求的地址
    curl_setopt($ch,CURLOPT_URL,'https://localhost/3.post.handl.php');
    //跳过跳过https证书验证
    curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
    //3.发出请求
    curl_exec($ch);

    //4.关闭资源
    curl_close($ch);

四:通过curl,模拟cookie登陆

<?php
    $ch = curl_init();
    $url = 'http://localhost/0402/index.php?c=user&a=doLogininAction';

    //设置发送地址,地址为登陆地址
    curl_setopt($ch,CURLOPT_URL,$url);
    $data = array(
        'user_name' => 'zhangsanfeng',
        'password' => 'admin123456'
    );

    //设置发送方式
    curl_setopt($ch,CURLOPT_POST,true);

    //设置发送数据
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);

    //获取cookie,设置我们的cookie保存到哪里:服务器要把session名字告诉你,此处为开启session时保存的sessionid
    curl_setopt($ch,CURLOPT_COOKIEJAR,'D:/cookie.txt');
    curl_exec($ch);

    curl_close($ch);

    //我们再访问index.php这个文件的时候,要随身携带cookie(保存的是session文件名)
    curl_setopt($ch,CURLOPT_URL,'http://localhost//0420/index.php');

    //设置携带的cookie
    curl_setopt($ch,CURLOPT_COOKIEFILE,'D:/cookie.txt');
    curl_exec($ch);
    curl_close($ch);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值