手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表2008年11月17日的文章

使用CURL函数发送数据时的注意事项

PHP函数库里面,提到CURL,恐怕很多人都会翘起大拇指吧,确实,这个函数库太牛叉了

CURL其实是调用的CURL的lib,随着PHP版本的升高,curl所需的lib版本也随之提高。

关于CURL所必须的类库和安装说明,手册上有详细介绍:

XML/HTML代码
  1. Requirements  
  2.   
  3. In order to use PHP's cURL functions you need to install the libcurl package. PHP requires that you use libcurl 7.0.2-beta or higher. In PHP 4.2.3, you will need libcurl version 7.9.0 or higher. From PHP 4.3.0, you will need a libcurl version that's 7.9.8 or higher. PHP 5.0.0 requires a libcurl version 7.10.5 or greater.  
  4.   
  5. Installation  
  6.   
  7. To use PHP's cURL support you must also compile PHP --with-curl[=DIR] where DIR is the location of the directory containing the lib and include directories. In the "include" directory there should be a folder named "curl" which should contain the easy.h and curl.h files. There should be a file named libcurl.a located in the "lib" directory. Beginning with PHP 4.3.0 you can configure PHP to use cURL for URL streams --with-curlwrappers.   
  8.   
  9. Note to Win32 Users: In order to enable this module on a Windows environment, libeay32.dll and ssleay32.dll must be present in your PATH.   
  10. You don't need libcurl.dll from the cURL site.   

然后在使用的时候也很方便,只需要初始化一下,设置一下postfields或者GET啥啥的,最后exec一下就行了。关键是别忘了close.

例子代码如下:

PHP代码
  1. $ch = curl_init("http://www.example.com/");  
  2. $fp = fopen("example_homepage.txt""w");  
  3.   
  4. curl_setopt($ch, CURLOPT_FILE, $fp);  
  5. curl_setopt($ch, CURLOPT_HEADER, 0);  
  6.   
  7. curl_exec($ch);  
  8. curl_close($ch);  
  9. fclose($fp);  

以上例子代码有点特殊,是因为他把网页内容进行了下载,同时生成一个文件。这是默认调用GET的方法。

其实,CURL更多的是用来处理POST数据、上传文件等功能

例子如下:

PHP代码
  1. <?   
  2. /*  
  3. * Author: Ron  
  4. * Released: August 4, 2007  
  5. * Description: An example of the disguise_curl() function in order to grab contents from a website while remaining fully camouflaged by using a fake user agent and fake headers.  
  6. */   
  7.   
  8. $url = 'http://www.ericgiguere.com/tools/http-header-viewer.html';   
  9.   
  10. // disguises the curl using fake headers and a fake user agent.   
  11. function disguise_curl($url)   
  12. {   
  13.   $curl = curl_init();   
  14.   
  15.   // Setup headers - I used the same headers from Firefox version 2.0.0.6   
  16.   // below was split up because php.net said the line was too long. :/   
  17.   $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";   
  18.   $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";   
  19.   $header[] = "Cache-Control: max-age=0";   
  20.   $header[] = "Connection: keep-alive";   
  21.   $header[] = "Keep-Alive: 300";   
  22.   $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";   
  23.   $header[] = "Accept-Language: en-us,en;q=0.5";   
  24.   $header[] = "Pragma: "// browsers keep this blank.   
  25.   
  26.   curl_setopt($curl, CURLOPT_URL, $url);   
  27.   curl_setopt($curl, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');   
  28.   curl_setopt($curl, CURLOPT_HTTPHEADER, $header);   
  29.   curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com');   
  30.   curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');   
  31.   curl_setopt($curl, CURLOPT_AUTOREFERER, true);   
  32.   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);   
  33.   curl_setopt($curl, CURLOPT_TIMEOUT, 10);   
  34.   
  35.   $html = curl_exec($curl); // execute the curl command   
  36.   curl_close($curl); // close the connection   
  37.   
  38.   return $html// and finally, return $html   
  39. }   
  40.   
  41. // uses the function and displays the text off the website   
  42. $text = disguise_curl($url);   
  43. echo $text;   
  44. ?>   

上面是一个比较完整的实现。特别需要注意的是header头部的发送,最初看手册的时候,我一以为CURLOPT_HTTPHEADER所需的数组是键值对应的,即:

PHP代码
  1. $header = array('Keep-Alive'=>'300');  

现实的残酷告诉我,不应该这么用,而是象上面的例子那样,每条header为数组的一个记录。

切记切记啊

Tags: curl, curlopt, httpheader, libcurl, charset