php 中使用cURL发送get/post请求,上传图片,批处理

https://mp.weixin.qq.com/s/8luqMEd8xt8oJxFLLCU1XA

文章正文

cURL是利用url语法规定传输文件和数据的工具。php中有curl拓展,一般用来实现网络抓取,模拟发送get   post请求,文件上传。

  在php中建立curl的基本步骤如下:

  1 初始化        2 设置选项,包括url        3  执行并获取结果     4 释放curl句柄。

  在工作和学习中,我也是时常用的curl。由于在使用curl设置选项时,各种选项比较难以记忆,需要参考,故在此记录下常用的一些例子,以便后来参考。

实例一 : 抓取网页数据(以拉手网开放api为例,也是get请求)

php
header("Content-type: text/html; charset=utf-8"); 
$ch = curl_init();//初始化

/*============开始设置curl各种选项================*/
curl_setopt($ch, CURLOPT_URL, "http://open.lashou.com/opendeals/lashou/city.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);


$html = curl_exec($ch);//执行句柄,获取返回内容

curl_close($ch);//释放句柄

echo $html
?>

如果用这种方法发get请求,参数附加到url后面即可

     如curl_setopt($ch, CURLOPT_URL, "http://localhost/tqj/date/p822.php?name=yyyyy");

实例 2  利用curl发送post请求

php
$uri = "http://localhost/tqj/date/p822.php";

// post参数数组
$data = array (
        'name' => 'tianquanjun',
        'password' => 'tianquanjun',
);

//初始化
$ch = curl_init ();

//各种项设置,网上参考而来,可以查看php手册,自己设置
curl_setopt ( $ch, CURLOPT_URL, $uri );
curl_setopt ( $ch, CURLOPT_POST, 1 );//post方式
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );

//执行
$return = curl_exec ( $ch );
//释放
curl_close ( $ch );

print_r($return);
?>

实例三  :curl 过程调试与错误信息处理

php
$uri = "http://localhost/tqj/date/p822.php";

// post参数数组
$data = array (
        'name' => 'tianquanjun',
        'password' => 'tianquanjun',
);

//初始化
$ch = curl_init ();

//各种项设置,网上参考而来,可以查看php手册,自己设置
curl_setopt ( $ch, CURLOPT_URL, $uri );
curl_setopt ( $ch, CURLOPT_POST, 1 );//post方式
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );

//执行
$return = curl_exec ( $ch );

//容错机制
if($return === false){
    var_dump(curl_error($ch));
    }

//curl_getinfo()获取各种运行中信息,便于调试 
$info = curl_getinfo($ch);

echo "执行时间".$info['total_time'].PHP_EOL;

//释放
curl_close ( $ch );

print_r($return);
?>

 

其中利用curl_error()获取错误信息,curl_getinfo()获取运行相关信息。

实例四  上传图片,获取返回信息。

跨域上传图片,同时获取返回信息,这个就能大显身手。和post比较像,注意文件之前加一个@符号

php
$uri = "http://localhost/tqj/date/p822.php";

// post参数数组
$data = array (
        'author' => 'tianquanjun',
        'upload' => '@C:\Users\tianquanjun.DANGDANG\Pictures\a.jpg',
);

//初始化
$ch = curl_init ();

//各种项设置,网上参考而来,可以查看php手册,自己设置
curl_setopt ( $ch, CURLOPT_URL, $uri );
curl_setopt ( $ch, CURLOPT_POST, 1 );//post方式
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );

//执行
$return = curl_exec ( $ch );

//容错机制
if($return === false){
    var_dump(curl_error($ch));
    }

//curl_getinfo()获取各种运行中信息,便于调试 
$info = curl_getinfo($ch);

echo "执行时间".$info['total_time'].PHP_EOL;

//释放
curl_close ( $ch );

print_r($return);
?>

实例五 : curl批处理。

  curl有一个高级特性,批处理句柄。允许打开多个curl链接。 

  批处理就是打开多个curl句柄,并把这些句柄指派给一个批处理句柄,然后在while循环里等待处理完毕。curl_multi_exec()算是称得上多线程处理,不过它还是属于异步的范畴。

php
header("Content-type: text/html; charset=gbk");
$urls=array('http://www.baidu.com','http://www.qq.com/');
$ch=array();
//批处理句柄
$mh=curl_multi_init();

//打开多个curl句柄,并指派给一个批处理句柄
$ch[0]=curl_init($urls[0]);
$ch[1]=curl_init($urls[1]);
for($i=0;$i<2;$i++)
{
curl_setopt($ch[$i],CURLOPT_RETURNTRANSFER,1);
curl_multi_add_handle($mh,$ch[$i]);

}
$running = NULL;
do{
    usleep(10000);
    curl_multi_exec($mh,$running);//实现批处理,可以看做curl多线程,实际是异步范畴
}while($running>0);
$res=array();
for($j=0;$j<2;$j++)
{
    $res[$j]=curl_multi_getcontent($ch[$j]);
}

//关闭句柄
for($k=0;$k<2;$k++)
{
    curl_multi_remove_handle($mh,$ch[$k]);
}
curl_multi_close($mh);

print_r($res);


?>

 

基本算是列举了常用的一些实例。要想灵活运用curl,还是得熟悉curl的各个设置项,这些设置项才是curl的灵魂。

==========================亲测效果===============================================

getwether.php

php
$data = "theCityName=邯郸";
$uri = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName";

//初始化
$obj = curl_init();

//设置
curl_setopt($obj, CURLOPT_URL, $uri);
curl_setopt($obj, CURLOPT_HEADER, 0);  //将头文件的信息作为数据流输出
curl_setopt($obj, CURLOPT_RETURNTRANSFER, 1); //执行后直接返回
curl_setopt($obj, CURLOPT_POST, 1);  //post 方式
curl_setopt($obj, CURLOPT_POSTFIELDS, $data);  //发送post请求参数
curl_setopt($obj,CURLOPT_USERAGENT, " user-agent:Mozilla/5.0 (Windows NT 6.1; rv:62.0) Gecko/20100101 Firefox/62.0");//浏览器头信息
curl_setopt($obj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded;charset=utf-8", "Content-Length:" . strlen($data))); //发送header头信息

$opt = curl_exec($obj);


if ($opt === false) {
    var_dump(curl_error($obj));
} else {
    var_dump(curl_errno($obj));//int(0)

    $info = curl_getinfo($obj);
    echo '执行时间:'.$info['total_time'] . PHP_EOL;
//    echo $opt;
    print_r($opt);
}

curl_close($obj);
var_dump(curl_errno($obj));//int(0)        返回最后一次的错误号
var_dump(curl_error($obj)); //string(0) "" 返回一个保护当前会话最近一次错误的字符串

在cmd命令行中输入:

php -f getwether.php >wether.txt
int(0)
执行时间:0.187


  <string>河北string>
  <string>邯郸string>
  <string>53892string>
  <string>53892.jpgstring>
  <string>2018/10/17 22:45:23string>
  <string>8℃/19℃string>
  <string>10月17日 多云string>
  <string>北风小于3级string>
  <string>1.gifstring>
  <string>1.gifstring>
  <string>今日天气实况:气温:12℃;风向/风力:北风 1级;湿度:77%;紫外线强度:弱。空气质量:中。string>
  <string>紫外线指数:弱,辐射较弱,涂擦SPF12-15、PA+护肤品。
健臻·血糖指数:易波动,血糖易波动,注意监测。
穿衣指数:较舒适,建议穿薄外套或牛仔裤等服装。
洗车指数:较适宜,无雨且风力较小,易保持清洁度。
空气污染指数:中,易感人群应适当减少室外活动。
string>
  <string>8℃/18℃string>
  <string>10月18日 多云string>
  <string>南风小于3级string>
  <string>1.gifstring>
  <string>1.gifstring>
  <string>9℃/19℃string>
  <string>10月19日 多云string>
  <string>南风小于3级string>
  <string>1.gifstring>
  <string>1.gifstring>
  <string>邯郸市是河北省最古老的城市,也是我国古代著名的城邑。早在2000多年前的春秋初期,就有邯郸的建制,当时邯郸已是一个人口聚居的城市。战国时作为赵国都城历经158年,秦统一后分别设邯郸县、邯郸郡。当时的邯郸经济昌盛、繁荣发达,与长安、洛阳、开封、成都同称五大都城。邯郸位于河北省南部,地处太行山东麓、淦阳河上,是晋冀鲁豫四省接壤的腹地。邯郸市位于太行山东麓,属暖温带半湿润半干旱大陆季风性气候。特点是四季分明,雨量适中,秋、春两季短,冬、夏两季长。邯郸市的自然环境得天独厚,西倚太行山,东临滏阳河,地势西北高、东南低,中部丘陵起伏,盆地交错;并有沁河、渚河、输元河流经市区(皆属滏阳河支流),丰富的水利资源为农业生产提供了优良的条件。景观:黄梁梦吕仙祠、永年君臣上寿石刻、学步桥、磁州窑、古邺城、兰陵王墓、天子冢、响堂山石窟、磁山古文化遗址等。string>

 ====================爬去豆瓣电影api==================================

php

$uri="https://api.douban.com/v2/movie/in_theaters";
$data="city=邯郸&start=0&count=3";

$obj=curl_init();

curl_setopt($obj,CURLOPT_URL,$uri);
curl_setopt($obj,CURLOPT_RETURNTRANSFER,1);
curl_setopt($obj,CURLOPT_POSTFIELDS,$data);
curl_setopt($obj,CURLOPT_POST,1);
curl_setopt($obj,CURLOPT_SSL_VERIFYPEER,0);
$opt=curl_exec($obj);

if($opt===false){
    var_dump(curl_error($obj));
}else{
    $info=curl_getinfo($obj);
   /* echo '
';
    print_r($info);
    echo '
';
*/ $movies=json_decode($opt,true); echo count($movies); echo '
';
    print_r($movies);
    echo '
'; } curl_close($obj);

结果:

5

Array
(
    [count] => 3
    [start] => 0
    [total] => 21
    [subjects] => Array
        (
            [0] => Array
                (
                    [rating] => Array
                        (
                            [max] => 10
                            [average] => 8.1
                            [stars] => 40
                            [min] => 0
                        )

                    [genres] => Array
                        (
                            [0] => 剧情
                            [1] => 动作
                            [2] => 犯罪
                        )

                    [title] => 无双
                    [casts] => Array
                        (
                            [0] => Array
                                (
                                    [alt] => https://movie.douban.com/celebrity/1044899/
                                    [avatars] => Array
                                        (
                                            [small] => https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p35678.jpg
                                            [large] => https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p35678.jpg
                                            [medium] => https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p35678.jpg
                                        )

                                    [name] => 周润发
                                    [id] => 1044899
                                )

                            [1] => Array
                                (
                                    [alt] => https://movie.douban.com/celebrity/1041390/
                                    [avatars] => Array
                                        (
                                            [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p49475.jpg
                                            [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p49475.jpg
                                            [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p49475.jpg
                                        )

                                    [name] => 郭富城
                                    [id] => 1041390
                                )

                            [2] => Array
                                (
                                    [alt] => https://movie.douban.com/celebrity/1016668/
                                    [avatars] => Array
                                        (
                                            [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p146.jpg
                                            [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p146.jpg
                                            [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p146.jpg
                                        )

                                    [name] => 张静初
                                    [id] => 1016668
                                )

                        )

                    [collect_count] => 325710
                    [original_title] => 無雙
                    [subtype] => movie
                    [directors] => Array
                        (
                            [0] => Array
                                (
                                    [alt] => https://movie.douban.com/celebrity/1014716/
                                    [avatars] => Array
                                        (
                                            [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p3555.jpg
                                            [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p3555.jpg
                                            [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p3555.jpg
                                        )

                                    [name] => 庄文强
                                    [id] => 1014716
                                )

                        )

                    [year] => 2018
                    [images] => Array
                        (
                            [small] => https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2535096871.jpg
                            [large] => https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2535096871.jpg
                            [medium] => https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2535096871.jpg
                        )

                    [alt] => https://movie.douban.com/subject/26425063/
                    [id] => 26425063
                )

            [1] => Array
                (
                    [rating] => Array
                        (
                            [max] => 10
                            [average] => 7.4
                            [stars] => 40
                            [min] => 0
                        )

                    [genres] => Array
                        (
                            [0] => 剧情
                            [1] => 动作
                            [2] => 武侠
                        )

                    [title] => 影
                    [casts] => Array
                        (
                            [0] => Array
                                (
                                    [alt] => https://movie.douban.com/celebrity/1274235/
                                    [avatars] => Array
                                        (
                                            [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p805.jpg
                                            [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p805.jpg
                                            [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p805.jpg
                                        )

                                    [name] => 邓超
                                    [id] => 1274235
                                )

                            [1] => Array
                                (
                                    [alt] => https://movie.douban.com/celebrity/1004856/
                                    [avatars] => Array
                                        (
                                            [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1415690807.36.jpg
                                            [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1415690807.36.jpg
                                            [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1415690807.36.jpg
                                        )

                                    [name] => 孙俪
                                    [id] => 1004856
                                )

                            [2] => Array
                                (
                                    [alt] => https://movie.douban.com/celebrity/1275564/
                                    [avatars] => Array
                                        (
                                            [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1366015827.84.jpg
                                            [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1366015827.84.jpg
                                            [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1366015827.84.jpg
                                        )

                                    [name] => 郑恺
                                    [id] => 1275564
                                )

                        )

                    [collect_count] => 255137
                    [original_title] => 影
                    [subtype] => movie
                    [directors] => Array
                        (
                            [0] => Array
                                (
                                    [alt] => https://movie.douban.com/celebrity/1054398/
                                    [avatars] => Array
                                        (
                                            [small] => https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p568.jpg
                                            [large] => https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p568.jpg
                                            [medium] => https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p568.jpg
                                        )

                                    [name] => 张艺谋
                                    [id] => 1054398
                                )

                        )

                    [year] => 2018
                    [images] => Array
                        (
                            [small] => https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2530513100.jpg
                            [large] => https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2530513100.jpg
                            [medium] => https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2530513100.jpg
                        )

                    [alt] => https://movie.douban.com/subject/4864908/
                    [id] => 4864908
                )

            [2] => Array
                (
                    [rating] => Array
                        (
                            [max] => 10
                            [average] => 5.1
                            [stars] => 25
                            [min] => 0
                        )

                    [genres] => Array
                        (
                            [0] => 喜剧
                        )

                    [title] => 李茶的姑妈
                    [casts] => Array
                        (
                            [0] => Array
                                (
                                    [alt] => https://movie.douban.com/celebrity/1363857/
                                    [avatars] => Array
                                        (
                                            [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1519453932.46.jpg
                                            [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1519453932.46.jpg
                                            [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1519453932.46.jpg
                                        )

                                    [name] => 黄才伦
                                    [id] => 1363857
                                )

                            [1] => Array
                                (
                                    [alt] => https://movie.douban.com/celebrity/1350408/
                                    [avatars] => Array
                                        (
                                            [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1437031126.82.jpg
                                            [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1437031126.82.jpg
                                            [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1437031126.82.jpg
                                        )

                                    [name] => 艾伦
                                    [id] => 1350408
                                )

                            [2] => Array
                                (
                                    [alt] => https://movie.douban.com/celebrity/1350407/
                                    [avatars] => Array
                                        (
                                            [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1437031175.04.jpg
                                            [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1437031175.04.jpg
                                            [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1437031175.04.jpg
                                        )

                                    [name] => 宋阳
                                    [id] => 1350407
                                )

                        )

                    [collect_count] => 112833
                    [original_title] => 李茶的姑妈
                    [subtype] => movie
                    [directors] => Array
                        (
                            [0] => Array
                                (
                                    [alt] => https://movie.douban.com/celebrity/1313050/
                                    [avatars] => Array
                                        (
                                            [small] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1518587170.5.jpg
                                            [large] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1518587170.5.jpg
                                            [medium] => https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1518587170.5.jpg
                                        )

                                    [name] => 吴昱翰
                                    [id] => 1313050
                                )

                        )

                    [year] => 2018
                    [images] => Array
                        (
                            [small] => https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2533384240.jpg
                            [large] => https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2533384240.jpg
                            [medium] => https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2533384240.jpg
                        )

                    [alt] => https://movie.douban.com/subject/27092785/
                    [id] => 27092785
                )

        )

    [title] => 正在上映的电影-邯郸
)

curl_getinfo函数获得信息:

php 中使用cURL发送get/post请求,上传图片,批处理_第1张图片

=============================================curl获得慕课用户信息=================================================

php
//C:\phpStudy\PHPTutorial\WWW\curl

date_default_timezone_set('PRC');


$data="username=用户名&password=密码&remember=1";
//15510485900 

$httpHeader=["Content-Type: application/x-www-form-urlencoded; charset=utf-8","Content-length:".strlen($data)];
$obj=curl_init();
curl_setopt($obj,CURLOPT_URL,"https://www.imooc.com/user/login");
//curl_setopt($obj,CURLOPT_URL,"https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.19)");
curl_setopt($obj,CURLOPT_RETURNTRANSFER,1);



curl_setopt($obj,CURLOPT_COOKIESESSION,true);
curl_setopt($obj,CURLOPT_COOKIEFILE,'cookiefile');
curl_setopt($obj,CURLOPT_COOKIEJAR,'cookiefile');
curl_setopt($obj,CURLOPT_COOKIE,session_name().'='.session_id());

curl_setopt($obj,CURLOPT_HEADER,0);
curl_setopt($obj,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($obj,CURLOPT_SSL_VERIFYPEER,false);//不需要认证
curl_setopt($obj,CURLOPT_POST,1);
curl_setopt($obj,CURLOPT_POSTFIELDS,$data);
curl_setopt($obj,CURLOPT_HTTPHEADER,$httpHeader);
curl_setopt($obj, CURLOPT_TIMEOUT, 300);
$return=curl_exec($obj);

if($return===false){
    var_dump(curl_error($obj));
}else{
    var_dump(curl_error($obj));
    var_dump(curl_errno($obj));
}
//echo $return;
//curl_close($obj);
//echo 'ceshi ';
//die;
curl_setopt($obj,CURLOPT_URL,"https://www.imooc.com/u/5510462/courses");
//curl_setopt($obj,CURLOPT_URL,"https://weibo.com/5549088156/profile?topnav=1&wvr=6&is_all=1");

curl_setopt($obj,CURLOPT_POST,0);
curl_setopt($obj,CURLOPT_HTTPHEADER,array("Content-type:text/xml;"));
$opt=curl_exec($obj);

/*if($opt===false){
    var_dump(curl_error($obj));
}else{
    var_dump(curl_error($obj));
    var_dump(curl_errno($obj));
}*/

curl_close($obj);

echo $opt;

结果:

string(0) ""
int(0)





"utf-8">
青春阳光爱伟的课程
"X-UA-Compatible" content="IE=edge, chrome=1">
"renderer" content="webkit" />

"qc:admins" content="77103107776157736375" />
"wb:webmaster" content="c4f857219bfae3cb" />
"Access-Control-Allow-Origin" content="*" />
"Cache-Control" content="no-transform " />
"Cache-Control" content="no-siteapp" />
"dns-prefetch" href="//www.imooc.com" />
"dns-prefetch" href="//img.imooc.com" />
"dns-prefetch" href="//img.mukewang.com" />
"apple-touch-icon" sizes="76x76" href="/static/img/common/touch-icon-ipad.png">
"apple-touch-icon" sizes="120x120" href="/static/img/common/touch-icon-iphone-retina.png">
"apple-touch-icon" sizes="152x152" href="/static/img/common/touch-icon-ipad-retina.png">
"keywords" content="慕课网,慕课官网,MOOC,移动开发,IT技能培训,免费编程视频,php开发教程,web前端开发,在线编程学习,html5视频教程,css教程,ios开发培训,安卓开发教程" />
"description" content="慕课网(IMOOC)是学习编程最简单的免费平台。慕课网提供了丰富的移动端开发、php开发、web前端、html5教程以及css3视频教程等课程资源。它富有交互性及趣味性,并且你可以和朋友一起编程。" />








"stylesheet" href="/static/moco/v1.0/dist/css/moco.min.css?t=201810221819" type="text/css" />
    "stylesheet" href="/static/lib/swiper/swiper-3.4.2.min.css?t=201810221819">










"stylesheet" href="/static/lib/layer/1.6.0/skin/layer.css" />



"stylesheet" href="//static.mukewang.com/static/css/??base.css,common/common-less.css?t=2.5,u/u_common_new-less.css,u/courses/time-less.css?v=201810221819" type="text/css" />



    

"header">
class="page-container" id="nav" >
class='search-warp clearfix' style='min-width: 32px; height: 72px;'>
class="search-area" data-search="top-banner"> class="search-input" data-suggest-trigger="suggest-trigger" type="text" autocomplete="off"> 'hidden' class='btn_search' data-search-btn="search-btn" />
    class="search-area-result" data-suggest-result="suggest-result">
class='showhide-search' data-show='no'>class='icon-search'>
class="newcomer index hide" href="//www.imooc.com/act/newcomer" target="_blank"> class="js-close imv2-close">
class="bindHintBox js-bindHintBox hide">
class="pr"> 为了账号安全,请及时绑定邮箱和手机"/user/setbindsns" class="ml20 color-red" target="_blank">立即绑定
class="arrow">
"main">
class="bg-other user-head-info">
class="user-info clearfix">
class="user-pic" data-is-fans="" data-is-follows="">
class="user-pic-bg"> class='img' src="//img3.mukewang.com/533e4c420001b2e502000200-140-140.jpg" alt=""/>
class="user-info-right">

class="user-name clearfix"> 青春阳光爱伟

class="about-info"> 北京 PHP开发工程师 class="more-user-info">class="imv2-arrow2_d">更多信息

class="user-sign hide">

class="user-desc" title="努力学习">努力学习

class="study-info clearfix">
class="item follows">
class="u-info-learn" title="学习时长167小时26分" style="cursor:pointer;"> 167h 学习时长
class="item follows"> 3532 经验
class="item follows"> 2 积分
class="item follows"> '/u/5510462/follows'>2 关注
class="item follows"> '/u/5510462/fans'>0 粉丝
class="item js-u-add-follow hide" data-uid='5510462' data-type="1" > class="status-btn add-follow ">class="icon-add">+关注
class="item js-u-already-follow hide" data-uid='5510462' data-type="2"> class="status-btn already-follow">class="imv2-check">已关注
class="item js-fans-msg hide" data-uid='5510462' data-type="3"> class="status-btn add-follow" >私信
class="wrap">
class="u-container">
class="c-tab clearfix">
class="js-course-list my-space-course study-tl">
class="clearfix tl-item tl-item-first"> class="time"> 2018 10月22日
class="course-list course-list-m">
class="clearfix tl-item "> class="time"> 2018 10月14日
class="course-list course-list-m">
class="clearfix tl-item "> class="time"> 2018 10月10日
class="course-list course-list-m">
class="clearfix tl-item "> class="time"> 2018 09月09日
class="course-list course-list-m">
class="clearfix tl-item "> class="time"> 2018 08月20日
class="course-list course-list-m">
class="clearfix tl-item "> class="time"> 2018 08月16日
class="course-list course-list-m">
class="clearfix tl-item "> class="time"> 2018 08月15日
class="course-list course-list-m">
class="clearfix tl-item "> class="time"> 2018 08月08日
class="course-list course-list-m">
class="clearfix tl-item "> class="time"> 2018 08月07日
class="course-list course-list-m">
class="clearfix tl-item "> class="time"> 2018 08月01日
class="course-list course-list-m">
class="clearfix tl-item "> class="time"> 2018 07月30日
class="course-list course-list-m">
class="clearfix tl-item "> class="time"> 2018 07月15日
class="course-list course-list-m">
class="clearfix tl-item "> class="time"> 2018 04月20日
class="course-list course-list-m">
class="clearfix tl-item tl-item-last"> class="time"> 2018 04月18日
class="course-list course-list-m">
"layer_sendmsg" class="font-colorg" style="display:none">
"display: none">

 ==================================curl从ftp下载文件=============================================

php
$obj=curl_init();

curl_setopt($obj,CURLOPT_URL,"ftp://dele.gotoftp3.com/wwwroot/404.php");
curl_setopt($obj,CURLOPT_HEADER,0);
curl_setopt($obj,CURLOPT_RETURNTRANSFER,1);
curl_setopt($obj,CURLOPT_TIMEOUT,300);//
curl_setopt($obj,CURLOPT_USERPWD,"dee:r9uj");//账号密码

$outfile=fopen('download.php','wb');//保存到本地文件
curl_setopt($obj,CURLOPT_FILE,$outfile);


$opt=curl_exec($obj);
fclose($outfile);
if (!curl_errno($obj)){
    echo 'return:'.$opt;
}else{
    echo 'curl error:'.curl_error($obj);

}
curl_close($obj);

 ===================================curl 往ftp上传文件===============================================

php
$obj=curl_init();
$fp=fopen('baidu.php','r');//文件句柄

curl_setopt($obj,CURLOPT_URL,"ftp://de.gotoftp3.com/wwwroot/baidu.txt");
curl_setopt($obj,CURLOPT_HEADER,0);
curl_setopt($obj,CURLOPT_RETURNTRANSFER,1);
curl_setopt($obj,CURLOPT_TIMEOUT,300);
curl_setopt($obj,CURLOPT_USERPWD,"dele:r9xdguj");

//对文件设置 操作,上传的文件,文件的大小
curl_setopt($obj,CURLOPT_UPLOAD,1);
curl_setopt($obj,CURLOPT_INFILE,$fp);
curl_setopt($obj,CURLOPT_INFILESIZE,filesize('baidu.php'));

$opt=curl_exec($obj);
fclose($fp);
if (!curl_errno($obj)){
    echo 'success upload';
}else{
    echo 'curl error:'.curl_error($obj);
}
curl_close($obj);

 ==============================curl 下载https中的数据==============================================

php
$obj=curl_init();
date_default_timezone_set('PRC');

curl_setopt($obj,CURLOPT_URL,"https://moco.imooc.com/captcha/script/captcha.min.js?v=1.0");
curl_setopt($obj,CURLOPT_RETURNTRANSFER,1);

curl_setopt($obj,CURLOPT_SSL_VERIFYPEER,0);
$opt=curl_exec($obj);
echo $opt;
curl_close($obj);

 

你可能感兴趣的:(php 中使用cURL发送get/post请求,上传图片,批处理)