Giter VIP home page Giter VIP logo

php-rest-curl's Introduction

PHP Rest CURL

PHP Rest CURL is a wrapper class for PHP, simplifying the typical CRUD requests to the extreme.

All the data passed to the server is encoded to JSON and decoded from JSON.

Getting Started

Using it is as simple as can be.

<?php
require_once('rest.inc.php');

// CALL
$result = RestCurl::get("https://api.mongolab.com/api/1/databases/my-db/collections/bookings?apiKey=0123456789abcde");

CRUD Methods

RestCurl supports the typical CRUD methods (GET, POST, PUT, DELETE) as follows:

// GET
$result = RestCurl::get($URL, array('id' => 12345678));

// POST
$result = RestCurl::post($URL, array('name' => 'RestCurl'));

// PUT
$result = RestCurl::put($URL, array('$set' => array('version' => 1)));

// DELETE
$result = RestCurl::delete($URL, array());	

The second parameter is an optional key/value array.

  • In GET requests its contents will be translated into query string parameters
    • array('id' => '12345678') will turn the URL into <url-prefix>/?id=12345678
    • If GET parameters are already present in the URL, nothing will be appended
  • For the rest of methods, the contents of the second parameter will be stringified as JSON and passed as the request body.

Return value

The previous example:

$res = RestCurl::get("https://api.mongolab.com/api/1/databases/my-db/collections/bookings?apiKey=0123456789abcde");

Will assign to $res something like:

print_r($res);
	
    Array
    (
        [status] => 200
        [data] => Array
            (
                [0] => stdClass Object
                    (
                        [_id] => stdClass Object
                            (
                                [$oid] => 52476ad1e4b08781144211f3
                            )

                        [city] => Barcelona
                        [date] => 2014-03-10
                        [guest] => Jordi Moraleda
                        [company] => Uniclau
                    )

            )
        [header] => HTTP/1.1 200 OK
    Date: Wed, 12 Mar 2014 01:17:53 GMT
    Server: Apache/2.2.22 (Ubuntu)
    Expires: Tue, 01 Feb 2000 08:00:00 GMT
    Last-Modified: Wed, 12 Mar 2014 01:17:53 GMT
    Cache-Control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0
    Pragma: no-cache
    X-Frame-Options: DENY
    Access-Control-Allow-Credentials: true
    Access-Control-Allow-Origin: *
    Transfer-Encoding: chunked
    Content-Type: application/json;charset=utf-8

    )

In the array:

  • $res['status'] is the HTTP status code.
  • $res['data'] is the JSON response parsed into an array
  • $res['header'] is a string with the response headers

Other HTTP methods

If you want to use methods besides GET, POST, PUT, DELETE, you can achieve that by just calling:

$result = RestCurl::exec('OPTIONS', $URL, array('foo' => 'bar'));

Dependencies

To get PHP Rest Curl working, you will need to install the corresponding package for your platform.

Debian/Ubuntu

sudo apt-get install curl libcurl3 libcurl3-dev php5-curl

Red Hat/CentOS

yum install php-common php-curl

Mac OS X (development)

  • Just use MAMP. Everything is bundled inside.

php-rest-curl's People

Contributors

brickpop 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

php-rest-curl's Issues

Add custom header

Add custom header and relateds, example code:

// Array $http_header data default

$headerDataDefault = array('Accept: application/json', 'Content-Type: application/json');
// Check headerData
if (!empty($headerData)) {

    $headerDataDefault = array_merge($headerDataDefault, $headerData);

}

curl_setopt($curl, CURLOPT_HTTPHEADER, $headerDataDefault);

Send custom header:

// $headerData = array('Authorization: HASH_AUTHORIZATION')
RestCurl::exec("GET", $url, $headerData, $obj);

stil working ?

because the filter is niet working

RestCurl::get($this->apiUrl, array('serial_number' => '12345'));

is not responding correctly

rest curl post get 400

iam try to use

$url = "https://graph.facebook.com/v2.6/me/messages?access_token=".$accessToken; RestCurl::post($url);

but get in status 400
how to use post method?

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.