Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
lib/curl_cookie.txt
lib/curl_cookie.txt
.idea/*
.DS_Store
4 changes: 2 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ Click the `download` link above or `git clone git://github.com/shuber/curl.git`

Simply require and initialize the `Curl` class like so:

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

$curl = new Curl;

### Performing a Request

Expand Down
13 changes: 13 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "duantianyu/curl",
"type": "library",
"description": "A basic CURL wrapper for PHP Edit",
"keywords": ["curl","php curl"],
"version": "0.1.1",
"require": {
"php": ">=5.3"
},
"autoload": {
"psr-4": {"Curl\\": "lib/"}
}
}
4 changes: 0 additions & 4 deletions curl.php

This file was deleted.

52 changes: 42 additions & 10 deletions lib/curl.php → lib/Curl.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

namespace Curl;

use Curl\CurlResponse;

/**
* A basic CURL wrapper
*
Expand All @@ -16,6 +20,13 @@ class Curl {
* @var string
**/
public $cookie_file;

/**
* The cookies string for requests
*
* @var string
**/
public $cookie;

/**
* Determines whether or not requests should follow redirects
Expand Down Expand Up @@ -44,7 +55,14 @@ class Curl {
* @var string
**/
public $referer;


/**
* The default timeout 5s
*
* @var int
**/
public $timeout = 5;

/**
* The user agent to send along with requests
*
Expand Down Expand Up @@ -74,7 +92,7 @@ class Curl {
* Sets the $cookie_file to "curl_cookie.txt" in the current directory
* Also sets the $user_agent to $_SERVER['HTTP_USER_AGENT'] if it exists, 'Curl/PHP '.PHP_VERSION.' (http://github.com/shuber/curl)' otherwise
**/
function __construct() {
public function __construct() {
$this->cookie_file = dirname(__FILE__).DIRECTORY_SEPARATOR.'curl_cookie.txt';
$this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Curl/PHP '.PHP_VERSION.' (http://github.com/shuber/curl)';
}
Expand All @@ -88,7 +106,7 @@ function __construct() {
* @param array|string $vars
* @return CurlResponse object
**/
function delete($url, $vars = array()) {
public function delete($url, $vars = array()) {
return $this->request('DELETE', $url, $vars);
}

Expand All @@ -97,7 +115,7 @@ function delete($url, $vars = array()) {
*
* @return string
**/
function error() {
public function error() {
return $this->error;
}

Expand All @@ -110,7 +128,7 @@ function error() {
* @param array|string $vars
* @return CurlResponse
**/
function get($url, $vars = array()) {
public function get($url, $vars = array()) {
if (!empty($vars)) {
$url .= (stripos($url, '?') !== false) ? '&' : '?';
$url .= (is_string($vars)) ? $vars : http_build_query($vars, '', '&');
Expand All @@ -127,7 +145,7 @@ function get($url, $vars = array()) {
* @param array|string $vars
* @return CurlResponse
**/
function head($url, $vars = array()) {
public function head($url, $vars = array()) {
return $this->request('HEAD', $url, $vars);
}

Expand All @@ -138,7 +156,7 @@ function head($url, $vars = array()) {
* @param array|string $vars
* @return CurlResponse|boolean
**/
function post($url, $vars = array()) {
public function post($url, $vars = array()) {
return $this->request('POST', $url, $vars);
}

Expand All @@ -151,7 +169,7 @@ function post($url, $vars = array()) {
* @param array|string $vars
* @return CurlResponse|boolean
**/
function put($url, $vars = array()) {
public function put($url, $vars = array()) {
return $this->request('PUT', $url, $vars);
}

Expand All @@ -165,15 +183,16 @@ function put($url, $vars = array()) {
* @param array|string $vars
* @return CurlResponse|boolean
**/
function request($method, $url, $vars = array()) {
public function request($method, $url, $vars = array()) {
$this->error = '';
$this->request = curl_init();
if (is_array($vars)) $vars = http_build_query($vars, '', '&');

$this->set_request_method($method);
$this->set_request_options($url, $vars);
$this->set_request_headers();

$this->set_request_timeout();

$response = curl_exec($this->request);

if ($response) {
Expand Down Expand Up @@ -244,6 +263,9 @@ protected function set_request_options($url, $vars) {
curl_setopt($this->request, CURLOPT_COOKIEFILE, $this->cookie_file);
curl_setopt($this->request, CURLOPT_COOKIEJAR, $this->cookie_file);
}
if($this->cookie){
curl_setopt($this->request,CURLOPT_COOKIE, $this->cookie);
}
if ($this->follow_redirects) curl_setopt($this->request, CURLOPT_FOLLOWLOCATION, true);
if ($this->referer) curl_setopt($this->request, CURLOPT_REFERER, $this->referer);

Expand All @@ -253,4 +275,14 @@ protected function set_request_options($url, $vars) {
}
}

/**
* Set timeout to the current request
*
* @return void
* @access protected
**/
protected function set_request_timeout() {
if(is_numeric($this->timeout)) curl_setopt($this->request, CURLOPT_TIMEOUT, $this->timeout);
}

}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PHP file should be the EOF.

2 changes: 2 additions & 0 deletions lib/curl_response.php → lib/CurlResponse.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Curl;

/**
* Parses the response from a Curl request into an object containing
* the response body and an associative array of headers
Expand Down