网页内容替换
$curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "http://www.baidu.com"); //执行后不直接打印出来 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($curl); curl_close($curl); echo str_replace("百度", "屌丝", $output);
通过webservice查询北京当前的天气
$data = ‘theCityName=北京‘; $curlobj = curl_init(); curl_setopt($curlobj, CURLOPT_URL, "http://www.webxml.com.cn/webServices/WeatherWebService.asmx/getWeatherbyCityName"); //不返回请求头信息 curl_setopt($curlobj, CURLOPT_HEADER, 0); //以文件流形式输出,不直接输出 curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1); //post方式请求 curl_setopt($curlobj, CURLOPT_POST, 1); //传递一个作为http ‘post’操作的所有数据的字符串 curl_setopt($curlobj, CURLOPT_POSTFIELDS, $data); //这句不写会报错(请求中包含user-agent头的字符串)--未将对象引用设置到对象的实例 curl_setopt($curlobj,CURLOPT_USERAGENT, $_SERVER[‘HTTP_USER_AGENT‘]); curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencode;charset=utf-8","Content-length:".strlen($data))); $rtn = curl_exec($curlobj); if(!curl_errno($curlobj)){ //$info = curl_getinfo($curlobj); //print_r($info); echo $rtn; }else{ echo ‘Curl error‘.curl_error($curlobj); } curl_close($curlobj);
把本地文件上传FTP服务器
$curlobj = curl_init(); $localfile = ‘ftp01.php‘; $fp = fopen($localfile, ‘r‘); curl_setopt($curlobj, CURLOPT_URL, "ftp://192.168.1.100/ftp01_uploaded_01.php"); curl_setopt($curlobj, CURLOPT_HEADER, 0); curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1); //最大延迟多少秒 curl_setopt($curlobj, CURLOPT_TIMEOUT, 300); curl_setopt($curlobj, CURLOPT_USERPWD, "peter.zhou:123456"); //让PHP为上传做准备 curl_setopt($curlobj, CURLOPT_UPLOAD, 1); //输出文件 curl_setopt($curlobj, CURLOPT_INFILE, filesize($localfile)); $rtn = curl_exec($curlobj); fclose($fp); if(!curl_errno($curlobj)){ echo "uploaded successfully"; }else{ echo ‘curl error:‘ . curl_error($curlobj); }
从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); curl_setopt($curlobj, CURLOPT_USERPWD, "peter.zhou:123456");//FTP用户名:密码 $outfile = fopen(‘test.txt‘, ‘wb‘);//保存到本地的文件名 //输出文件 curl_setopt($curlobj, CURLOPT_FILE, $outfile); $rtn = curl_exec($curlobj); curl_close($curlobj); if(!curl_errno($curlobj)){ //$info = curl_getinfo($curlobj); //print_r($info); echo $rtn; }else{ echo ‘Curl error‘.curl_error($curlobj); }
下载网络上的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, true); //设置HTTPS支持 date_default_timezone_set(‘PRC‘); curl_setopt($curlobj,CURLOPT_SSL_VERIFYPEER,0); $output = curl_exec($curlobj); curl_close($curlobj); echo $output;
登录慕课网并下载个人空间页面
$data = ‘[email protected]&password=123&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"); curl_setopt($curlobj, CURLOPT_COOKIESESSION, true); 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); curl_setopt($curlobj, CURLOPT_POSTFIELDS, $data); curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencode;charset=utf-8","Content-length:".strlen($data))); curl_exec($curlobj); curl_setopt($curlobj, CURLOPT_POST, "http://www.imooc.com/sapce/index"); curl_setopt($curlobj, CURLOPT_POST, 0); curl_setopt($curlobj, CURLOPT_HTTPHEADER, 0); curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("Content-type:text/xml")) curl_close($curlobj); echo $output;
时间: 2024-10-24 12:44:44