Coder Social home page Coder Social logo

curl's Introduction

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]

curl's People

Contributors

shuber avatar weppos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

curl's Issues

multiple headers cause wrong $reponse->body wrong

When a url return content by redirect once. The response will have two headers. And the CurlReponse just preg_match one header. and the other exists in the $reponse->body. I thought it's a bug.

Thank U.

server didn't get $_POST when $curl->post(url,data)

hi,
When i make a command line curl -d "data=plop" url , my server running php script get the $_POST filed with data=plop but when i use your library, i must make a file_get_content(php://input) to get the data.

Why ? :(

HTTP/2 failures

Hello! Seems there is a problem with HTTP/2 request. The pattern in the preg regex isn't able to detect the new HTTP/2 response. Maybe the pattern needs to be updated or a check for which version was sent to check the response.

Namespacing

Hi shuber, thanks for this useful wrapper.

Have you considered adding namespaces on it? Following any standard like PSR-0 would be great.

Remove all headers from response when multiple headers are received

When a request is completed following a redirect, the curl response contains the headers from the first and the second requests. In this case not all the headers are removed, only the last one is stripped from the response, resulting in a body containing the other headers.

HTTP/1.1 302 Found
Location: http://www.google.es/
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Date: Tue, 20 Dec 2011 10:54:50 GMT
Server: gws
Content-Length: 218
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN

HTTP/1.1 200 OK
Date: Tue, 20 Dec 2011 10:54:50 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked

Curl certificate form Windows Server

In case Curl is used on Windows, it may be required to pass a certificate to the curl command like so :

In the set_request_options() method, found in the Curl class

// Add certificate for Windows
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
curl_setopt($this->request, CURLOPT_CAINFO, dirname(FILE) . DIRECTORY_SEPARATOR . 'Curl' . DIRECTORY_SEPARATOR . 'cacert.pem');
}

  1. You can obtain the certificate at http://curl.haxx.se/docs/caextract.html
  2. The piece of code above suggest creating a Curl/ folder at the component's root Directory

By the way, many thanks for this repo ;)

Upload files to Rest api

Hi shuber,

I have an upload api i use it to upload files to google cloud storage and this is the api

http://acquired-backup-198200.appspot.com/upload?uname=arun&Description=description&players=arun,sai&community=true&Location=US&skills=batting&analysis=true

I use php-curl to upload files to this api and this is the php code

`if( $_SERVER['REQUEST_METHOD'] == 'POST' ){

	$file = $_FILES['files'];
	$fileName = $file['name'];
	$fileType = $file['type'];
	$fileTmp = $file['tmp_name'];
	var_dump( $file );
	$curl = curl_init();
	curl_setopt_array($curl, array(
	  CURLOPT_URL => "http://acquired-backup-198200.appspot.com/upload?uname=arun&Description=description&players=arun,sai&community=true&Location=US&skills=batting&analysis=true",
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => "",
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 30,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => "POST",
	  CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"filename\"; filename=\"". $fileName ."\"\r\nfile=\"filedir\"; filedir=\"@/home/alaa/Desktop/Cielo_estrellado_by_Eduardo_Diez_Viñuela.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
  CURLOPT_HTTPHEADER => array(
    "Cache-Control: no-cache",
    "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
};
}` 

And this is html

<form method="post" enctype="multipart/form-data"> <input type="file" name="files"> <input type="submit" name=""> </form>

The process succeed but the files in the storage are 0 bytes size so can you help me in that problem please?

POST timeout

Hello. Is there any way to detect if a curl POST timeouts?

Request response add curl_getinfo

if ($response) {
$response = new CurlResponse($response);
$response->info = curl_getinfo($this->request);
} else {
$this->error = curl_errno($this->request).' - '.curl_error($this->request);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.