CURL

php的curl扩展, 模拟客户端发出http请求报文

<?php
    //1. 初始化curl,返回资源
    $curl = curl_init();    
    //2. 设置curl工具请求的服务器文件地址
    //参数1:curl资源
    //参数2:设置的请求的选项
    //参数3:请求选项的值
    //设置将结果返回而不是直接显示
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    //跳过百度的https证书的验证
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);//不验证
    curl_setopt($curl, CURLOPT_HEADER, 0);      //不返回header头信息

    curl_setopt($curl, CURLOPT_URL, 'https://www.baidu.com/index.php');

    //3. 发出请求,接收返回的数据
    $result = curl_exec($curl);
    echo '<pre>';
    var_dump($result);

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

POST请求

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

    //2. 设置选项,设置请求的服务器地址
    curl_setopt($ch, CURLOPT_URL, 'http://localhost/0420curl/3.post_handle.php');

    //开启post提交
    curl_setopt($ch, CURLOPT_POST, true);
    //设置提交什么数据过去
    $data = [
    'order'=>'ESC1000012',
    //>=php5.6
    'img'=> new \CURLFile('/path/to/x.jpg'),
    //<php5.6
    'img'=>'@/path/to/x.jpg',
    ];
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    //设置cookie存储路径
    curl_setopt($ch, CURLOPT_COOKIEJAR, '/path/to/x.cookie');
    //设置cookie读取路径
    curl_setopt($ch, CURLOPT_COOKIEFILE, '/path/to/x.cookie');

    //3. 发出请求
    curl_exec($ch);

    //4. 关闭资源
    curl_close($ch);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值