PHP CURL方法封装
//--------------------------------------------------------------------------------------
/**
* 函数的含义说明:CURL发送post请求 获取数据
*
* @access public
* @param str $url 发送接口地址
* @param array/json $data 要发送的数据
* @param false/true $json false $data数组格式 true $data json格式
* @return 返回json数据
*/
public function httpPostRequest($url, $data = null,$json = FALSE){
//创建了一个curl会话资源,成功返回一个句柄;
$curl = curl_init();
//设置url
curl_setopt($curl, CURLOPT_URL, $url);
//设置为FALSE 禁止 cURL 验证对等证书(peer’s certificate)
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
//设置为 1 是检查服务器SSL证书中是否存在一个公用名(common name)
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
//设置请求为POST
curl_setopt($curl, CURLOPT_POST, 1);
//curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60); //最长的可忍受的连接时间
//设置POST的数据域
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
if($json){
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data)
)
);
}
}
//设置是否将响应结果存入变量,1是存入,0是直接输出
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
//然后将响应结果存入变量
$output = curl_exec($curl);
//关闭这个curl会话资源
curl_close($curl);
return $output;
}
//--------------------------------------------------------------------------------------
/**
* 函数的含义说明:CURL发送get请求 获取数据
*
* @access public
* @param str $url 发送接口地址 https://api.douban.com/v2/movie/in_theaters?city=广州&start=0&count=10
* @return 返回json数据
*/
public function httpGetRequest($url){
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
//curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
$output = curl_exec($curl); //返回api的json对象
//关闭URL请求
curl_close($curl);
return $output; //返回json对象
}
//--------------------------------------------------------------------------------------
/**
* 函数的含义说明:模仿登录带cookie值抓取页面
* @access public
* @param string
* @return string
*/
$data=array('username' => 'promonkey',
'password' => '1q2w3e',
'remember'=>1);
$data='username=zjzhoufy@126.com&password=1q2w3e&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;
//--------------------------------------------------------------------------------------
/**
* 函数的含义说明:模仿登录获取cookie值抓取页面
* @access public
* @param string
* @return string
*/
$data='username=demo_peter@126.com&password=123qwe&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);
// 注释掉这行,因为这个设置必须关闭安全模式 以及关闭open_basedir,对服务器安全不利
//curl_setopt($curlobj, CURLOPT_FOLLOWLOCATION, 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)
));
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_redir_exec($curlobj); // 执行
curl_close($curlobj); // 关闭cURL
echo $output;
//------------------------------------------------------------------------------------
CURL登录携带cookie获取首页数据配置
date_default_timezone_set('PRC');//使用cookie,必须先设置时区
curl_setopt($curlobj,CURLOPT_COOKIESESSION,RURE);
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支持页面跳转
//------------------------------------------------------------------------------------
/**
* 实例描述:从FTP服务器下载一个文件到本地
*/
$curlobj = curl_init();
curl_setopt($curlobj, CURLOPT_URL, "ftp://192.168.1.100/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, "peter.zhou:123456");//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);
//------------------------------------------------------------------------------------
/**
* 代码实例-PHP-cURL实战
* 实例描述:把本地文件上传到FTP服务器上
*/
$curlobj = curl_init();
$localfile = 'ftp01.php';
$fp = fopen($localfile, 'r');
curl_setopt($curlobj, CURLOPT_URL, "ftp://192.168.1.100/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, "peter.zhou: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);
//------------------------------------------------------------------------------------
/**
* 代码实例-PHP-CURL下载文件或者图片
*$url:远程图片路径或文件路径
*$path:本地下载路径
*/
public function download($url, $path = 'images/')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$file = curl_exec($ch);
curl_close($ch);
$filename = pathinfo($url, PATHINFO_BASENAME);
$resource = fopen($path . $filename, 'a');
fwrite($resource, $file);
fclose($resource);
}
版权声明:
作者:超级管理员
链接:
https://blog.apecloud.ltd/article/detail.html?id=81
来源:猿码云个人技术站
文章版权归作者所有,未经允许请勿转载。
THE END
二维码
打赏
共有0条评论