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

全部博文(207)

文章存档

2009年(200)

2008年(7)

我的朋友

分类:

2009-03-31 23:41:47

cURL or Client URL Library is a very powerful tool, and its something that i recently had to use while working with two APIs one for and one for .

PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP’s ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.

These functions have been added in PHP 4.0.2.

You can interact with most APIs using cURL, at first it seams intimidating but once you start to use it you become comfortable with the set up.

Lets get started by parsing an RSS feed.

  1. $ch = curl_init();  

curl_init() initializes a new session and return a cURL handle for use with the curl_setopt(), curl_exec(), and curl_close() functions. We set $ch with the curl_init() function to get things rolling.

  1. curl_setopt($ch, CURLOPT_URL, $feed);   
  2. curl_setopt($ch, CURLOPT_HEADER, 0);   
  3. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   
  4. curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);  

Just like the name says, curl_setopt() will set an option on the given cURL session handle. There are far to many to list so you are bette roff reading the manual found here. In our example we are going to need to access a URL and bring back the data.

CURLOPT_URL - This will be the location of the RSS file.
CURLOPT_HEADER - We set this to 0 or false as we do not want to see the header information.
CURLOPT_RETURNTRANSFER - This will transfer the data back as a string instead of outputting directly to the browser.
CURLOPT_USERAGENT - While this is not needed, The contents of the “User-Agent: ” header to be used in a HTTP request.

  1. $rss = curl_exec($ch);   
  2. curl_close($ch);  

Once we have our options set the next step is to execute them using curl_exec(), we will asign $rss to curl_exec() thus giving the variable the contents of our look up.

For good measure we close the connection using the curl_close() function.

Since we know RSS feeds are made of XML we can goa head and do some simple parsing of my XML.

  1. // Manipulate string into object   
  2. $rss = simplexml_load_string($rss);  

is available in PHP 5.1.0 and on and will take a well-formed XML string and returns it as an object. This makes it easy for us to work with.

I have talked about simplexml_load_string() in .

Since my goal was to introduce you to cURL i’m not going to go into much detail with the XML parsing. we have set an array on the variable $rss, i usually use Print_r($rss) to see the structure of the array before i work with it, this makes it easy to work with.

  1. $cnt = count($rss->channel->item);   
  2.   
  3. for($i=0; $i<$cnt$i++)   
  4. {   
  5.     $url = $rss->channel->item[$i]->link;   
  6.     $title = $rss->channel->item[$i]->title;   
  7.     $desc = $rss->channel->item[$i]->description;   
  8.     echo '

    .$url.'">'.$title.'

    '
    .$desc.'';   
  9. }  

The above code is counting the number of items and looping through them one at a time printing the data to the screen, not real magic here.

Below is the complete source code for your coding pleasure.

  1.   
  2. Function feedMe($feed) {   
  3.     // Use cURL to fetch text   
  4.     $ch = curl_init();   
  5.     curl_setopt($ch, CURLOPT_URL, $feed);   
  6.     curl_setopt($ch, CURLOPT_HEADER, 0);   
  7.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   
  8.     curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);   
  9.     $rss = curl_exec($ch);   
  10.     curl_close($ch);   
  11.   
  12.     // Manipulate string into object   
  13.     $rss = simplexml_load_string($rss);   
  14.   
  15.     $siteTitle = $rss->channel->title;   
  16.     echo "

    ".$siteTitle."

    "
    ;   
  17.     echo "";   
  18.   
  19.     $cnt = count($rss->channel->item);   
  20.   
  21.     for($i=0; $i<$cnt$i++)   
  22.     {   
  23.         $url = $rss->channel->item[$i]->link;   
  24.         $title = $rss->channel->item[$i]->title;   
  25.         $desc = $rss->channel->item[$i]->description;   
  26.         echo '

    .$url.'">'.$title.'

    '
    .$desc.'';   
  27.     }   
  28. }   
  29.   
  30. feedMe("");  

cURL is a powerful tool, you can interact with APIs as well as sites or services that don’t have an AIL like Google Analytics. So play with it, make a twitter feed parser :)

Further Reading:
http://ca.php.net/curl
http://www.web-development-blog.com/archives/tutorial-ftp-upload-via-curl/

阅读(587) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~