一波PHP中cURL库的常见用法代码示例


Posted in PHP onMay 06, 2016

php 的CURL是不错的功能,下面收藏几段不错的片段

0、基本例子
一般流程:

$to_url=$_GET['url'];
print_r($_GET);
if(substr($to_url,0,1)=='/'){
 $to_url="http://www.amazon.com".$to_url;
}
echo $to_url;
//初始化
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, $to_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
//执行并获取HTML文档内容
$output = curl_exec($ch);
$output=preg_replace("#href=\"#","href=\"http://in2.qq-ex.com/amazon.php?url=",$output);
// 释放curl句柄
curl_close($ch);
echo $output;
// 指定代理地址
curl_setopt($ch, CURLOPT_PROXY, '11.11.11.11:8080');
// 如果需要的话,提供用户名和密码
curl_setopt($ch, CURLOPT_PROXYUSERPWD,'user:pass');

1、测试网站是否运行正常

if (isDomainAvailible('http://gz.itownet.cn')) 
  { 
    echo "Up and running!"; 
  } 
  else 
  { 
    echo "Woops, nothing found there."; 
  } 
 
  //returns true, if domain is availible, false if not 
  function isDomainAvailible($domain) 
  { 
    //check, if a valid url is provided 
    if(!filter_var($domain, FILTER_VALIDATE_URL)) 
    { 
      return false; 
    } 
 
    //initialize curl 
    $curlInit = curl_init($domain); 
    curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10); 
    curl_setopt($curlInit,CURLOPT_HEADER,true); 
    curl_setopt($curlInit,CURLOPT_NOBODY,true); 
    curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true); 
 
    //get answer 
    $response = curl_exec($curlInit); 
 
    curl_close($curlInit); 
 
    if ($response) return true; 
 
    return false; 
  }

2、可以代替file_gecontents的操作

function file_get_contents_curl($url) { 
 $ch = curl_init(); 
 
 curl_setopt($ch, CURLOPT_HEADER, 0); 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser. 
 curl_setopt($ch, CURLOPT_URL, $url); 
 
 $data = curl_exec($ch); 
 curl_close($ch); 
 
 return $data; 
}

3、保存某个网站下的所有图片

function getImages($html) { 
 $matches = array(); 
 $regex = '~http://somedomain.com/images/(.*?)\.jpg~i'; 
 preg_match_all($regex, $html, $matches); 
 foreach ($matches[1] as $img) { 
  saveImg($img); 
 } 
} 
 
function saveImg($name) { 
 $url = 'http://somedomain.com/images/'.$name.'.jpg'; 
 $data = get_data($url); 
 file_put_contents('photos/'.$name.'.jpg', $data); 
} 
 
$i = 1; 
$l = 101; 
 
while ($i < $l) { 
 $html = get_data('http://somedomain.com/id/'.$i.'/'); 
 getImages($html); 
 $i += 1; 
}

4、FTP应用

// open a file pointer 
$file = fopen("/path/to/file", "r"); 
 
// the url contains most of the info needed 
$url = "ftp://username:password@mydomain.com:21/path/to/new/file"; 
 
$ch = curl_init(); 
 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
 
// upload related options 
curl_setopt($ch, CURLOPT_UPLOAD, 1); 
curl_setopt($ch, CURLOPT_INFILE, $fp); 
curl_setopt($ch, CURLOPT_INFILESIZE, filesize("/path/to/file")); 
 
// set for ASCII mode (e.g. text files) 
curl_setopt($ch, CURLOPT_FTPASCII, 1); 
 
$output = curl_exec($ch); 
curl_close($ch);

5、使用curl发送JSON数据

$data = array("name" => "Hagrid", "age" => "36");                                   
$data_string = json_encode($data);                                           
  
$ch = curl_init('http://api.local/rest/users');                                    
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                    
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                    
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                      
  'Content-Type: application/json',                                         
  'Content-Length: ' . strlen($data_string))                                     
);                                                           
  
$result = curl_exec($ch);
PHP 相关文章推荐
PHP.MVC的模板标签系统(二)
Sep 05 PHP
JAVA/JSP学习系列之四
Oct 09 PHP
php配置php-fpm启动参数及配置详解
Nov 04 PHP
Codeigniter生成Excel文档的简单方法
Jun 12 PHP
php中get_cfg_var()和ini_get()的用法及区别
Mar 04 PHP
FleaPHP框架数据库查询条件($conditions)写法总结
Mar 19 PHP
浅谈php和js中json的编码和解码
Oct 24 PHP
微信开发之php表单微信中自动提交两次问题解决办法
Jan 08 PHP
PHP中的自动加载操作实现方法详解
Aug 06 PHP
PHP 自动加载类原理与用法实例分析
Apr 14 PHP
Laravel 框架控制器 Controller原理与用法实例分析
Apr 14 PHP
PHP实现计算器小功能
Aug 28 PHP
PHP实现的简单分页类及用法示例
May 06 #PHP
PHP实现的多文件上传类及用法示例
May 06 #PHP
PHP动态地创建属性和方法, 对象的复制, 对象的比较,加载指定的文件,自动加载类文件,命名空间
May 06 #PHP
php将一维数组转换为每3个连续值组成的二维数组
May 06 #PHP
PHP递归遍历多维数组实现无限分类的方法
May 06 #PHP
PHP图片加水印实现方法
May 06 #PHP
php数组冒泡排序算法实例
May 06 #PHP
You might like
PHP has encountered an Access Violation 错误的解决方法
2010/01/17 PHP
探讨:如何通过stats命令分析Memcached的内部状态
2013/06/14 PHP
php提交表单发送邮件的方法
2015/03/20 PHP
php 函数中静态变量使用的问题实例分析
2020/03/05 PHP
jquery 实现的全选和反选
2009/04/15 Javascript
js实现俄罗斯方块小游戏分享
2014/01/31 Javascript
jquery中常用的函数和属性详细解析
2014/03/07 Javascript
jQuery增加与删除table列的方法
2016/03/01 Javascript
JavaScript蒙板(model)功能的简单实现代码
2016/08/04 Javascript
对js eval()函数的一些见解
2016/08/15 Javascript
ThinkJS中如何使用MongoDB的CURD操作
2016/12/13 Javascript
vue.js选中动态绑定的radio的指定项
2017/06/02 Javascript
vue2.0 下拉框默认标题设置方法
2018/08/22 Javascript
vue 中引用gojs绘制E-R图的方法示例
2018/08/24 Javascript
AutoJs实现刷宝短视频的思路详解
2020/05/22 Javascript
vue自定义指令限制输入框输入值的步骤与完整代码
2020/08/30 Javascript
python中base64加密解密方法实例分析
2015/05/16 Python
R vs. Python 数据分析中谁与争锋?
2017/10/18 Python
Python中一行和多行import模块问题
2018/04/01 Python
Python3爬楼梯算法示例
2019/03/04 Python
Python time库基本使用方法分析
2019/12/13 Python
python模拟哔哩哔哩滑块登入验证的实现
2020/04/24 Python
python实现二分类和多分类的ROC曲线教程
2020/06/15 Python
俄罗斯电子产品在线商店:UltraTrade
2020/01/30 全球购物
垃圾回收的优点和原理。并考虑2种回收机制
2016/10/16 面试题
人力资源管理专业应届生求职信
2013/09/28 职场文书
护理专业本科生自荐信
2013/10/01 职场文书
军训心得体会
2013/12/31 职场文书
秋季运动会稿件
2014/01/30 职场文书
节能环保标语
2014/06/12 职场文书
商场周年庆活动方案
2014/08/19 职场文书
医院护士见习期自我鉴定
2014/09/15 职场文书
师德标兵先进事迹材料
2014/12/19 职场文书
紧急通知
2015/04/17 职场文书
写给女朋友的保证书
2015/05/09 职场文书
element tree树形组件回显数据问题解决
2022/08/14 Javascript