Chinaunix首页 | 论坛 | 博客
  • 博客访问: 589845
  • 博文数量: 207
  • 博客积分: 10128
  • 博客等级: 上将
  • 技术积分: 2440
  • 用 户 组: 普通用户
  • 注册时间: 2004-10-10 21:40
文章分类

全部博文(207)

文章存档

2009年(200)

2008年(7)

我的朋友

分类:

2009-04-25 07:21:41

Using cURL is a simple and effective way to gather data from another website, run it through a script, parse the data and transform it into something useful that you can use on your website. Whether you are “scraping” data to build a summary of a link, pulling an XML file to parse into a database, or just simply wanting to get the contents of the file, cURL is a simple and effective way to pull the data from an outside source into your page.

Making sure cURL is enabled and setup
First things first, you need to make sure cURL is enabled on your web host. The easiest way to accomplish this is to check your phpinfo on your server. Simply deploy a PHP file with the following information onto your server, and name it whatever you want.

  1. phpinfo();   
  2. ?>  

After the file is uploaded/saved onto your web server, look through the file to ensure that there is a section that looks as follows.

phpinfo_curl

If your PHP file doesn’t have this section of code, or nothing similar to it, then your hosting service may not support cURL, or it may not be enabled. If you are on a hosting service, you can ask your host to enable it for you, or if you are on your own server, you can modify your php.ini file to enable the extension.

You can modify your php.ini file as follows:
(if you can’t find it, look at the top of the script we wrote above, it will give you the ini path)

  1. // Find this line in your php.ini   
  2. ;extension=php_curl.dll   
  3.   
  4. // Remove the semi-colon in front, to make the line look like this:   
  5. extension=php_curl.dll  

After modifying and saving your php.ini file, you are going to have to restart your web service.

- If you are running on Apache, you should be able to enable it with a simple “apachectl restart” command.

- If you are running an IIS web server, you are going to have to restart IIS or just restart the Worker Pool that is running your PHP. This can be done through the MMC IIS Snap-In.

- If you are running WAMP on your local machine, simply right-click on the WAMP icon in your system tray, find the Apache menu, and click “Restart”.

Just make sure you go back into your file running phpinfo() to ensure that cURL is showing up in the file now. If not, you may want to seek addition support from your IT, Co-workers or Web hosting provider for more information as to why cURL will not function on your server.

Assuming everything is running now, and cURL is enabled, we will continue onwards.

A simple cURL Request

cURL isn’t incredibly hard to use to pull the data in, as illustrated below.

  1. // Init $curl as a cURL object   
  2. $curl = curl_init();   
  3.   
  4. // Tell cURL what URL we are going after   
  5. curl_setopt($curl, CURLOPT_URL, '');   
  6.   
  7. // Tell cURL we would like headers as well   
  8. curl_setopt($curl, CURLOPT_HEADER, 1);   
  9.   
  10. // Tell cURL we would like the results as a string instead of just dumping it on the screen   
  11. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);   
  12.   
  13. // Execute the cURL request   
  14. $data = curl_exec($curl);   
  15.   
  16. // Close the cURL request   
  17. curl_close($curl);   
  18.   
  19. // Display the data from the variable to ensure its there.   
  20. var_dump($data);  
阅读(903) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~