Xinu

php cURL使用
1. curl 模拟登录<?php /** * curl模拟登录 * Created by PhpSto...
扫描右侧二维码阅读全文
29
2018/01

php cURL使用

1. curl 模拟登录

<?php
/**
 * curl模拟登录
 * Created by PhpStorm.
 * User: Xiny i@xiny9.com
 * Date: 2018-01-29
 * Time: 20:59
 */
$ch = curl_init();
$data = "user=admin&pass=123456";
//设置访问的链接
curl_setopt($ch,CURLOPT_URL,'http://xxx.xxx.com/login');
//不直接输出内容
curl_setopt($ch,CURLOPT_RETURNTRANSFER,'1');
//Cookie设置,需要设置在访问之前
//cookie有时效性,需要设置同一时区
date_default_timezone_set("PRC");
$cookie = tempnam('./temp', 'cookie');
curl_setopt($ch,CURLOPT_COOKIESESSION,TRUE);
curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie);
curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie);
curl_setopt($ch,CURLOPT_COOKIE,session_name().'='.session_id());
//不打印Header头信息
curl_setopt($ch,CURLOPT_HEADER,0);
//curl开启页面跳转
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
//开启post访问
curl_setopt($ch,CURLOPT_POST,1);
//传递post数据
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
//设置请求头信息
$header = [
    'Accept:*/*',
    'application/x-www-form-urlencoded;charset=utf-8',
    'Content-length:'.strlen($data)
    //'Accept-Encoding:gzip, deflate, br',
    //'Accept-Language:zh-CN,zh;q=0.9',
    //'Host:www.xx.xx',
    //'Referer:https://xx.xx.com',//访问来源
    //'X-Requested-With:XMLHttpRequest',//ajax访问
    //'CLIENT-IP:109.246.184.28', //访问ip
    //'X-FORWARDED-FOR:109.246.184.28' //访问ip
];
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
curl_exec($ch);
//重置访问链接
curl_setopt($ch,CURLOPT_URL,'http://xxx.xxx.com/login');
curl_setopt($ch,CURLOPT_HTTPHEADER,['Content-type:text/html']);
//使用get访问
curl_setopt($ch,CURLOPT_POST,0);
$output = curl_exec($ch);
echo $output;

2. curl ftp文件下载

<?php
/**
 * curl ftp文件下载
 * Created by PhpStorm.
 * User: Xiny i@xiny9.com
 * Date: 2018-01-29
 * Time: 20:59
 */
$curlobj = curl_init();
curl_setopt($curlobj, CURLOPT_URL, "ftp://192.168.1.1/downloaddemo.txt");
curl_setopt($curlobj, CURLOPT_HEADER, 0);
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlobj, CURLOPT_TIMEOUT, 300); // times out after 300s
curl_setopt($curlobj, CURLOPT_USERPWD, "Xiny:123456");//FTP用户名:密码
// Sets up the output file
$outfile = fopen('dist.txt', 'wb');//保存到本地的文件名
curl_setopt($curlobj, CURLOPT_FILE, $outfile);

$rtn = curl_exec($curlobj);
fclose($outfile);
if(!curl_errno($curlobj)){
    // $info = curl_getinfo($curlobj);
    // print_r($info);
    echo "RETURN: " . $rtn;
} else {
  echo 'Curl error: ' . curl_error($curlobj);
}
curl_close($curlobj);
?>

3. curl ftp文件上传

<?php
/**
 * curl ftp文件上传
 * Created by PhpStorm.
 * User: Xiny i@xiny9.com
 * Date: 2018-01-29
 * Time: 20:59
 */
$curlobj = curl_init();
$localfile = 'ftp01.php';
$fp = fopen($localfile, 'r');

curl_setopt($curlobj, CURLOPT_URL, "ftp://192.168.1.1/ftp01.php");  
curl_setopt($curlobj, CURLOPT_HEADER, 0);
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlobj, CURLOPT_TIMEOUT, 300); // times out after 300s
curl_setopt($curlobj, CURLOPT_USERPWD, "Xiny:123456");//FTP用户名:密码

curl_setopt($curlobj, CURLOPT_UPLOAD, 1);
curl_setopt($curlobj, CURLOPT_INFILE, $fp);
curl_setopt($curlobj, CURLOPT_INFILESIZE, filesize($localfile));
$rtn = curl_exec($curlobj);
fclose($fp);
if(!curl_errno($curlobj)){
    echo "Uploaded successfully.";
} else {
  echo 'Curl error: ' . curl_error($curlobj);
}
curl_close($curlobj);
?>

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
不检查ssl证书,值为0或者2,默认为2,检查证书,校验域名

Last modification:January 29th, 2018 at 09:47 pm

2 comments

  1. 啦啦德玛西亚

  2. 赛斯

Leave a Comment