layout: post
title: "cURL"
date: 2016-05-12 20:52:24 +0800
comments: true
categories:
cURl的概念
cURL(Client URL Library Functions)使用URL语法传输数据的命令行工具
使用场景
-
网页资源
- 网页爬虫
-
WebService数据接口资源
- 动态获取接口数据,如天气 号码归属地等
-
FTP服务器里面的文件资源
- 下载Ftp里面的文件
-
其他资源
- 所有网络上的资源都可以用cURL访问和下载
确定是否支持cURLphp -i |grep cURL
cURL操作步骤
- 初始化
- curl_init()
- 执行
- curl_exec()
- 关闭
- curl_close()
实例
- 简单的网页爬虫
<?php
$curl=curl_init('http://www.baidu.com');
curl_exec($curl);
curl_close($curl);
?>
- 抓取网页并替换部分内容
<?php
/**
* 在网络上下载一个网页并把内容中的“百度”替换为“GOOD”之后输出
*/
$curlobj = curl_init(); // 初始化
curl_setopt($curlobj, CURLOPT_URL, "http://www.baidu.com"); // 设置访问网页的URL
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true); // 执行之后不直接打印出来
$output=curl_exec($curlobj); // 执行
curl_close($curlobj); // 关闭cURL
echo str_replace("百度","GOOD",$output);
?>
- 调用WebService获取天气信息
代码需要修改
<?php
$data = sprintf('<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getWeatherbyCityName xmlns="http://WebXml.com.cn/">
<theCityName>%s</theCityName>
</getWeatherbyCityName>
</soap:Body>
</soap:Envelope>','北京');
$curlobj = curl_init();
curl_setopt($curlobj, CURLOPT_URL, "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
curl_setopt($curlobj, CURLOPT_POST, 1);
curl_setopt($curlobj, CURLOPT_HEADER, 0);
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlobj, CURLOPT_POSTFIELDS, $data);
curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("Content-Type: application/soap+xml; charset=utf-8",
"Content-length: ".strlen($data),
"SOAPAction:\"http://WebXml.com.cn/getWeatherbyCityName\""));
$rtn = curl_exec($curlobj);
if(!curl_errno($curlobj)){
$info = curl_getinfo($curlobj);
print_r($info);
echo "RETURN: " . $rtn;
} else {
echo 'Curl error: ' . curl_error($curlobj);
}
curl_close($curlobj);
?>
- 登录网站并下载个人空间页面
<?php
/**
* 登录慕课网并下载个人空间页面
*/
$data=array('username' => 'promonkey',
'password' => '1q2w3e',
'remember'=>1);
$data='username=xxx&password=xxx&remember=1';
$curlobj = curl_init(); // 初始化
curl_setopt($curlobj, CURLOPT_URL, "http://www.imooc.com/user/login"); // 设置访问网页的URL
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true); // 执行之后不直接打印出来
// Cookie相关设置,这部分设置需要在所有会话开始之前设置
date_default_timezone_set('PRC'); // 使用Cookie时,必须先设置时区
curl_setopt($curlobj, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($curlobj, CURLOPT_HEADER, 0);
curl_setopt($curlobj, CURLOPT_FOLLOWLOCATION, 1); // 这样能够让cURL支持页面链接跳转
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)
));
curl_exec($curlobj); // 执行
curl_setopt($curlobj, CURLOPT_URL, "http://www.imooc.com/space/index");
curl_setopt($curlobj, CURLOPT_POST, 0);
curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("Content-type: text/xml"
));
$output=curl_exec($curlobj); // 执行
curl_close($curlobj); // 关闭cURL
echo $output;
?>
- FTP服务器下载文件
<?php
/**
* 从FTP服务器下载一个文件到本地
*/
$curlobj = curl_init();
curl_setopt($curlobj, CURLOPT_URL, "ftp://xxx(ip)/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, "xxx:xxx");//FTP用户名:密码
// Sets up the output file
$outfile = fopen('dest.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);
?>
- 上传文件到FTP
<?php
/**
* 把本地文件上传到FTP服务器上
*/
$curlobj = curl_init();
$localfile = 'ftp01.php';
$fp = fopen($localfile, 'r');
curl_setopt($curlobj, CURLOPT_URL, "ftp://xxx/ftp01_uploaded.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, "xxx:xxx");//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);
?>
- 访问HTTPS资源
<?php
/**
* 下载网络上面的一个HTTPS的资源
*/
$curlobj = curl_init(); // 初始化
curl_setopt($curlobj, CURLOPT_URL, "https://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.js"); // 设置访问网页的URL
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true); // 执行之后不直接打印出来
// 设置HTTPS支持
date_default_timezone_set('PRC'); // 使用Cookie时,必须先设置时区
curl_setopt($curlobj, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查从证书中检查SSL加密算法是否存在
curl_setopt($curlobj, CURLOPT_SSL_VERIFYHOST, 2); //
$output=curl_exec($curlobj); // 执行
curl_close($curlobj); // 关闭cURL
echo $output;
?>
网友评论