PHP中cURL使用
PHP中的数据传输神器cURL
先谢慕课网,谢谢老师。
cURL的概念:
(client URL Library Function ) 使用URL语法传输数据的命令行工具.客户端向服务器请求资源的工具。
cURL的使用场景:
请求网页资源(编写网页爬虫);
WebService数据接口资源(动态获取接口数据,比如天气,号码归属地等);
FTP服务器里面的文件资源(下载甚至上传FTP服务器里面的文件);
其他资源(所有网络上的资源都可以用cURL访问和下载到)。
在PHP中使用cURL:
1.确认PHP支持cURL;
(windows在命令行里面 php -i查看php相关信息,然后在cmd窗口上面右键编辑查找cURL,)
(Linux里面直接 php安装路径/php -i |grep cURL);
2.有就有,没有就安装,安装方式之前有总结。
3.步骤:初始化 curl_init(),发送请求,接收数据 curl_exec(),关闭curl curl_close()
用cURL制作一个简单的网页爬虫
$curl = curl_init("http://www.baidu.com");
curl_exec($curl);
curl_close($curl);
用cURL抓取网页信息并替换部分内容
$curlobj = curl_init();
curl_setopt($curlobj, CURLOPT_URL, "http://www.baidu.com");
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curlobj);
curl_close($curlobj);
echo str_replace('百度','呵呵',$output);
用cURL调用WebService获取天气信息
用post的方式获取数据
$data = "theCityCode=0376&theUserId=12";
$curlobj = curl_init();
curl_setopt($curlobj, CURLOPT_URL, "http://www.WebXml.com.cn/WebServices/WeatherWS.asmx/getWeather");
curl_setopt($curlobj, CURLOPT_HEADER, 0);
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlobj, CURLOPT_POST, 1);
curl_setopt($curlobj, CURLOPT_POSTFIELDS, $data);
curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded;
charset = utf-8",
"Content-length:".strlen($data)));
$rtn = curl_exec($curlobj);
if(!curl_error($curlobj)){
echo "RETURN:".$rtn;
} else {
echo "curl error: ".curl_error($curlobj);
}
用cURL下载需要登录的网页
保存cookie,做两次操作,第一步保存cookie做登录
$data = "username = yhy&password = 123456&remember = 1";
$curlobj = curl_init();
curl_setopt($curlobj, CURLOPT_URL, "http://www.imooc.com/user/login");
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);//不让直接打印
//cookie相关设置,cookie用于保存网站登录信息的媒介
date_default_timezone_set('PRC');//设置时区,因为cookie是有过期时间的
curl_setopt($curlobj, CURLOPT_COOKIESSION, TRUE);//支持cookie和session
curl_setopt($curlobj, CURLOPT_COOKIEFILE, "cookiefile");
//保存到本地的文件,文件名可调整
curl_setopt($curlobj, CURLOPT_COOKIEJAR, "cookiefile");
curl_setopt($curlobj, CURLOPT_COOKIE, session_name().'='.session_id());
curl_setopt($curlobj, CURLOPT_HEADER, 0);//不要打印头部信息
curl_setopt($curlobj, CURLOPT_FOLLOWLOCATION, 1);//支持页面链接跳转
curl_setopt($curlobj, CURLOPT_POST, 1);//post方式
curl_setopt($curlobj, CURLOPT_POSTFIELDS, $data);//传入数据
curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded;
charset = utf-8",
"Content-length:".strlen($data)));//长度信息
curl_exec($curlobj);//执行
curl_setopt($curlobj, CURLOPT_URL, "http://www.imooc.com/space/index");//重新初始化
curl_setopt($curlobj, CURLOPT_POST, 0);//不用post方式
curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("Content-type:text/xml");
$outfile = curl_exec($curlobj);//执行
curl_close($curlobj);
echo $output;
用cURL从ftp上下载文件到本地
$curlobj = curl_init();
curl_setopt($curlobj, CURLOPT_URL, "ftp://目标ip/down.txt");
curl_setopt($curlobj, CURLOPT_HEADER, 0);
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlobj, CURLOPT_TIMEOUT, 300);//超时时间设置
curl_setopt($curlobj, CURLOPT_USERPWD, "ftpuser:password");//设置ftp的账号密码
$outfile = fopen("dest.txt", 'wb');//写入本地的文件
curl_setopt($curlobj, CURLOPT_FILE, $outfile);
$rtn = curl_exec($curlobj);
fclose($outfile);
if(!curl_error($curlobj)){
echo "RETURN:".$rtn;
} else {
echo "curl error: ".curl_error($curlobj);
}
curl_close($curlobj);
用cURL从本地上传文件到ftp服务器
$curlobj = curl_init();
$localfile = "./put.txt";
$fp = fopen($localfile, 'r');
curl_setopt($curlobj, CURLOPT_URL, "ftp://目标ip/put01.txt");
curl_setopt($curlobj, CURLOPT_HEADER, 0);
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlobj, CURLOPT_TIMEOUT, 300);//超时时间设置
curl_setopt($curlobj, CURLOPT_USERPWD, "ftpuser:password");//设置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_error($curlobj)){
echo "Uploaded successfully";
} else {
echo "Curl error: ".curl_error($curlobj);
}
curl_close($curlobj);
用cURL访问HTTPS资源
$curlobj = curl_init();//初始化
curl_setopt($curlobj, CURLOPT_URL, "https://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.js");//设置访问地址
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);//执行之后不直接打印出来
date_default_timezone_set('PRC');//设置时区
curl_setopt($curlobj, CURLOPT_SSL_VERIFYPEER, 0);//终止从服务器进行验证
$output = curl_exec($curlobj);//执行
curl_close($curlobj);//关闭curl
echo $output;