• Stars
    star
    448
  • Rank 97,180 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 16 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A basic CURL wrapper for PHP

curl

A basic CURL wrapper for PHP (see http://php.net/curl for more information about the libcurl extension for PHP)

Installation

Click the download link above or git clone git://github.com/shuber/curl.git

Usage

Initialization

Simply require and initialize the Curl class like so:

require_once 'curl.php';
$curl = new Curl;

Performing a Request

The Curl object supports 5 types of requests: HEAD, GET, POST, PUT, and DELETE. You must specify a url to request and optionally specify an associative array or string of variables to send along with it.

$response = $curl->head($url, $vars = array());
$response = $curl->get($url, $vars = array()); # The Curl object will append the array of $vars to the $url as a query string
$response = $curl->post($url, $vars = array());
$response = $curl->put($url, $vars = array());
$response = $curl->delete($url, $vars = array());

To use a custom request methods, you can call the request method:

$response = $curl->request('YOUR_CUSTOM_REQUEST_TYPE', $url, $vars = array());

All of the built in request methods like put and get simply wrap the request method. For example, the post method is implemented like:

function post($url, $vars = array()) {
    return $this->request('POST', $url, $vars);
}

Examples:

$response = $curl->get('google.com?q=test');

# The Curl object will append '&some_variable=some_value' to the url
$response = $curl->get('google.com?q=test', array('some_variable' => 'some_value'));

$response = $curl->post('test.com/posts', array('title' => 'Test', 'body' => 'This is a test'));

All requests return a CurlResponse object (see below) or false if an error occurred. You can access the error string with the $curl->error() method.

The CurlResponse Object

A normal CURL request will return the headers and the body in one response string. This class parses the two and places them into separate properties.

For example

$response = $curl->get('google.com');
echo $response->body; # A string containing everything in the response except for the headers
print_r($response->headers); # An associative array containing the response headers

Which would display something like

<html>
<head>
<title>Google.com</title>
</head>
<body>
Some more html...
</body>
</html>

Array
(
    [Http-Version] => 1.0
    [Status-Code] => 200
    [Status] => 200 OK
    [Cache-Control] => private
    [Content-Type] => text/html; charset=ISO-8859-1
    [Date] => Wed, 07 May 2008 21:43:48 GMT
    [Server] => gws
    [Connection] => close
)

The CurlResponse class defines the magic __toString() method which will return the response body, so echo $response is the same as echo $response->body

Cookie Sessions

By default, cookies will be stored in a file called curl_cookie.txt. You can change this file's name by setting it like this

$curl->cookie_file = 'some_other_filename';

This allows you to maintain a session across requests

Basic Configuration Options

You can easily set the referer or user-agent

$curl->referer = 'http://google.com';
$curl->user_agent = 'some user agent string';

You may even set these headers manually if you wish (see below)

Setting Custom Headers

You can set custom headers to send with the request

$curl->headers['Host'] = 12.345.678.90;
$curl->headers['Some-Custom-Header'] = 'Some Custom Value';

Setting Custom CURL request options

By default, the Curl object will follow redirects. You can disable this by setting:

$curl->follow_redirects = false;

You can set/override many different options for CURL requests (see the curl_setopt documentation for a list of them)

# any of these will work
$curl->options['AUTOREFERER'] = true;
$curl->options['autoreferer'] = true;
$curl->options['CURLOPT_AUTOREFERER'] = true;
$curl->options['curlopt_autoreferer'] = true;

Testing

Uses ztest, simply download it to path/to/curl/test/ztest (or anywhere else in your php include_path)

Then run test/runner.php

Contact

Problems, comments, and suggestions all welcome: [email protected]

More Repositories

1

sortable

Allows you to sort ActiveRecord items in multiple lists with multiple scopes
Ruby
147
star
2

proxy

Allows rails applications to respond to multiple hosts/domains and proxied requests
Ruby
100
star
3

vim-promiscuous

Instant context switching using git and vim sessions.
Vim Script
73
star
4

queryable_array

Provides a simplified DSL allowing arrays of objects to be searched by their attributes
Ruby
70
star
5

subdomain_account

A rails plugin that handles subdomain accounts
Ruby
59
star
6

postgres-twitter

Experimental build of a simple "twitter" app in Postgres
PLpgSQL
57
star
7

validates_as_hostname_label

Checks for valid hostname labels (i.e. subdomains)
Ruby
49
star
8

svnignore

git style ignores with subversion
Ruby
45
star
9

interface

Implementable interfaces in ruby
Ruby
31
star
10

event_calendar

Generates html event calendars with ruby
Ruby
30
star
11

authorization

authorization for rails
Ruby
26
star
12

hattr_accessor

Allows you to define attr_accessors that reference members of a hash
Ruby
26
star
13

variables

`Variable` objects for class and instance variables
Ruby
22
star
14

abstract_class

Abstract classes in Ruby
Ruby
20
star
15

eigenclass

Eigenclasses (metaclasses) in Ruby
Ruby
17
star
16

owners

Take ownership of your code
Ruby
16
star
17

defined

Calls Module#defined after a ruby class or module is (re)defined
Ruby
15
star
18

monolith

Monolithic git repository generator
Ruby
13
star
19

sub_diff

Inspect the changes of your `String#sub` and `String#gsub` replacements
Ruby
13
star
20

phuby

A port of ruby 2.0 to native php 5.4+
PHP
11
star
21

authentication

authentication for rails
Ruby
10
star
22

tmux-git

Display git information in your tmux status line
Shell
9
star
23

kalimba

JavaScript
8
star
24

restful_rails

An extendable PHP library to communicate with RESTful rails applications
PHP
5
star
25

nestable

Allows you to nest ActiveRecord records with various strategies like tree, set, path, etc.
Ruby
4
star
26

shuber.github.com

CSS
3
star
27

cache

A simple php caching library with swappable backends
PHP
3
star
28

woot

Scrapes woot.com sites with ruby
Ruby
3
star
29

pg_brainfuck

plpgsql brainfuck implementation
2
star
30

sudoku

Solves traditional square Sudoku puzzles
Ruby
2
star
31

gitserver

node gitserver
CoffeeScript
2
star
32

respond_to_missing

Defines Object#respond_to_missing? and patches Object#respond_to?
Ruby
2
star
33

goldberg

messing around with game physics in rubygame
Ruby
1
star
34

weeports

A tiny reporting application for php >= 5.3
PHP
1
star
35

jquery-favicons

Adds favicons to links
JavaScript
1
star
36

philt

PHP
1
star
37

myspace-google_analytics

Adds Google Analytics to your profile
1
star
38

smart_home

Ruby
1
star
39

taggie

The tiniest little HTML/XML parser...using regex
Ruby
1
star