正在加载...

PF BLOG

老外写的:Curl library for CodeIgniter

没时间翻译了,能用到的人肯定能看懂:

So, let’s continue to develop Mass PageRank Checker tool. I will use CodeIgniter (CI) PHP framework for it. It’s really simple and very powerful thing. But first I need Curl library for CI to make easier working with HTTP queries.

In brief, the library must do the following things:

  1. Send HTTP GET/POST requests and receive HTTP responses.
  2. Get information about queries like HTTP error code, total time for operation, number of sent/received bytes for operation, etc.
  3. Handle the errors.

I just wrote this library, you can download it here.(如果这个链接不能用,下载文章结尾的附件链接)

To use it just copy Curl.php to CI’s system/application/libraries folder, curl_lang.php to CI’s system/application/language/english folder and load it from your controllers:


$this->load->library('curl');

For example let’s simulate the search in Google:


$keyword = 'travel';
$this->load->library('curl');
$this->curl->open();
$content = $this->curl->http_get("http://www.google.com/search?q=$keyword");
$this->curl->close();
echo $content;

This is very simple example. At this moment the library has not powerful features. Here the list of what you can do and how you can do using Curl library:

  • Set additional options like User-Agent (MSIE 7.0 by default), timeout for operations in seconds (30 by default).

$this->curl->open(array('user_agent' => 'CURL library', 'timeout' => 60));
  • Send HTTP GET query and get the content. Setting $headers_only = TRUE will cause you receive HTTP headers only. You can specify additional HTTP headers in $headers variable.

function http_get($url, $headers = array(), $headers_only = FALSE)
  • Send HTTP POST query and get the content. POST parameters must be specified in $fields variable like ‘a=1&b=2&str=test%20example’

function http_post($url, $fields, $headers = array(), $headers_only = FALSE)
  • There are a few functions for getting different information about query

function get_http_code()
function get_total_time()
function get_bytes_uploaded()
function get_bytes_downloaded()
function get_speed_upload()
function get_speed_download()

In the future I’m planning to add the following features to library: referer and autoreferer support, cookies, proxies, authorization, follow location feature, caching, file uploading, additional headers and may be some more.

附件:curl.zip

——————————————————————————————————————————————

最新跟踪报道:

Curl library 1.1 for CodeIgniter

Curl library 1.1 for CodeIgniterI continue to develop Curl library for CodeIgniter and today’s blog post is about the new version. What’s new in this release?
First of all, request and response headers are supported now. You may specify your own request header in http_get and http_post methods:
 

$headers = array(
  'Authorization: AuthSub token="' . $_POST['token'] . '"',
  'X-Google-Key: key=' . $developerKey,
  'Content-Type: application/atom+xml'
);
$this->curl->http_post($url, $postfields, $headers);

As for response headers, you have an access to them now through the $headers variable:
 

echo $this->curl->headers;

One more feature that I want to tell you is automatic redirect support. Look at this code:
 

$this->curl->http_get('http://google.com');

Curl library will parse headers (or use CURLOPT_FOLLOWLOCATION curl option if possible) and automatically redirect to http://www.google.com.
To know what’s the final URL of HTTP GET/POST operation you can use $url variable:
 

echo $this->curl->url;

If for some reasons you do not want to use automatic redirect feature, use this code:
 

$this->curl->open(array('follow_location' => false)); // during initialization
// or
$this->curl->follow_location = false;

And the last thing I want to tell is a small bonus: abs_url function which will help you to convert relative URL to absolute one.
 

$absolute_url = $this->curl->abs_url('../test.html', 'http://example.com/path/to');

1.1版附件(据作者声称修复了若干小bug):curl_library_1_1_1.zip

Tags: codeigniter , curl

« 上一篇 | 下一篇 »

访客评论

  1. #1 TaoGOGO 2010-05-24, 9:09 PM
    你觉得ixwebhosting的空间怎么样?给个评价,可以到我的博客上说说,因为最近我想买个空间,提前谢谢啦

发表评论

评论内容 (必填):