一波PHP中cURL库的常见用法代码示例
2016-05-06 00:00
1001 查看
php 的CURL是不错的功能,下面收藏几段不错的片段
0、基本例子
一般流程:
1、测试网站是否运行正常
2、可以代替file_gecontents的操作
3、保存某个网站下的所有图片
4、FTP应用
5、使用curl发送JSON数据
php之curl实现http与https请求的方法
PHP扩展CURL的用法详解
PHP CURL获取返回值的方法
PHP中CURL的CURLOPT_POSTFIELDS参数使用细节
php使用curl访问https示例分享
PHP curl 获取响应的状态码的方法
PHP CURL获取cookies模拟登录的方法
PHP CURL CURLOPT参数说明(curl_setopt)
PHP中使用cURL实现Get和Post请求的方法
php中使用Curl、socket、file_get_contents三种方法POST提交数据
php运行出现Call to undefined function curl_init()的解决方法
无法加载php_curl.dll解决办法
php的curl实现get和post的代码
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之curl设置超时实例php之curl实现http与https请求的方法
PHP扩展CURL的用法详解
PHP CURL获取返回值的方法
PHP中CURL的CURLOPT_POSTFIELDS参数使用细节
php使用curl访问https示例分享
PHP curl 获取响应的状态码的方法
PHP CURL获取cookies模拟登录的方法
PHP CURL CURLOPT参数说明(curl_setopt)
PHP中使用cURL实现Get和Post请求的方法
php中使用Curl、socket、file_get_contents三种方法POST提交数据
php运行出现Call to undefined function curl_init()的解决方法
无法加载php_curl.dll解决办法
php的curl实现get和post的代码
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- php7 读取php.ini[4]
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得
- apache 环境下 php 的配置注意事项
- 简单好用的PHP分页类
- ASP.NET、ASP、PHP、JSP之间有什么区别?
- PHP VBS JS 函数 对照表