服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - 一波PHP中cURL库的常见用法代码示例

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

2021-01-15 17:45jackyrong PHP教程

这篇文章主要介绍了一波PHP中cURL库的常见用法代码示例,类Unix世界的cURL内置于PHP中,使Linux和Mac OS用户倍感亲切,需要的朋友可以参考下

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

0、基本例子
一般流程:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$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、测试网站是否运行正常

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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的操作

?
1
2
3
4
5
6
7
8
9
10
11
12
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、保存某个网站下的所有图片

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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应用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 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数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
$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批量去除文件UTF8 BOM信息

    如何使用PHP批量去除文件UTF8 BOM信息

    如果PHP文件头部包含BOM信息,就会输出一个空白,在很多时候会带来问题,比如我们session无法工作、cookie无法设置等等问题 ...

    PHP教程网3922020-05-13
  • PHP教程全面了解PHP中的全局变量

    全面了解PHP中的全局变量

    下面小编就为大家带来一篇全面了解PHP中的全局变量。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    PHP教程网6992021-01-28
  • PHP教程PHP中empty,isset,is_null用法和区别

    PHP中empty,isset,is_null用法和区别

    最近在阅读项目的源码,发现源码中就对empty、isset和is_null函数(语言特性)乱用,有的地方很明显的就挖坑了。不能正确的去理解这些东西,就很可能给...

    PHP教程网11562021-04-25
  • PHP教程php 可变函数使用小结

    php 可变函数使用小结

    PHP 支持可变函数的概念。这意味着如果一个变量名后有圆括号,PHP 将寻找与变量的值同名的函数,并且尝试执行它。本文重点给大家介绍php 可变函数使用...

    烟熏妆5202019-09-27
  • PHP教程php设计模式之单例模式实例分析

    php设计模式之单例模式实例分析

    这篇文章主要介绍了php设计模式之单例模式,实例分析了单例模式的原理与相关使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    蓝祖生5392020-09-06
  • PHP教程简单实现PHP留言板功能

    简单实现PHP留言板功能

    这篇文章主要教大家如何简单实现PHP留言板功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    尘中客10282021-04-08
  • PHP教程thinkphp 抓取网站的内容并且保存到本地的实例详解

    thinkphp 抓取网站的内容并且保存到本地的实例详解

    这篇文章主要介绍了thinkphp 抓取网站的内容并且保存到本地的实例详解的相关资料,需要的朋友可以参考下...

    modou9442021-06-21
  • PHP教程教你如何开启shopnc b2b2c 伪静态

    教你如何开启shopnc b2b2c 伪静态

    最近要给一个shopnc网站开启伪静态,用的是shopnc b2b2c,在网上搜索了好多shopnc开启伪静态的方法,但都是针对shaopnc c2c的,没有关于shopnc b2b2c的,最后终于...

    PHP开发实例3962020-08-01