Book Image

RESTful PHP Web Services

By : Samisa Abeysinghe
Book Image

RESTful PHP Web Services

By: Samisa Abeysinghe

Overview of this book

<p>Representational State Transfer (REST) is a form of software architecture; systems that follow it are often called RESTful. It is the architectural style that drives the World Wide Web (the motivation for REST was to capture elements that made the Web successful), and RESTful web services use simple protocols already familiar to web developers. RESTful web services are found in lots of places with big name Web APIs such as Flickr, and Yahoo search following a REST-based approach.<br /><br />With any architectural style or approach, struggling with its basic concepts is just one problem – implementation in your favorite language is another, and that's where this book comes in. If you're a PHP developer, this book will show you how to create and consume RESTful web services in PHP, and make your services work well in the context of the Web.<br /><br />You will learn about the basic concepts of REST, and then look at PHP tools, libraries and techniques for working with RESTful web services. You will see how to use other RESTful services from your PHP applications, and also how to engineer your PHP applications to make them RESTful.<br /><br />This book is about implementing RESTful web services in PHP, and so the book is packed with example code and careful explanations.</p>
Table of Contents (15 chapters)
RESTful PHP Web Services
Credits
About the Author
About the Reviewers
Preface
Index

Complete RESTClient Class


<?php
class RESTClient {
    private $with_curl;

    const USER_AGENT = 'RESTClient';

    /*
     * Constructor of the RESTClient
     */
    public function __construct() {
        if (function_exists("curl_init")) {
            $this->with_curl = TRUE;
        } else {
            $this->with_curl = FALSE;
        }
    }

    /*
     * Call the HTTP 'GET' method
     * @param string $url URL of the service.
     * @param array $params request parameters, hash of (key,value) pairs
     * @return response string
     */
    public function get($url, $params) {
        $params_str = "?";
        if (is_array($params)) {
            foreach ($params as $key => $value) {
                $params_str .= urlencode($key) . "=" . urlencode($value) . "&";
            }
        } else {
            $params_str .= $params;
        }

        $url .= $params_str;

        $result = "";

        if ($this->with_curl) {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HTTPGET, TRUE);
            curl_setopt($curl, CURLOPT_USERAGENT, RESTClient :: USER_AGENT);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
            $result = curl_exec($curl);
            curl_close($curl);
        } else {
            $opts = array (
                'http' => array (
                    'method' => "GET",
                    'header' => "User-Agent: " . RESTClient :: USER_AGENT . "\r\n"
                )
            );

            $context = stream_context_create($opts);

            $fp = fopen($url, 'r', false, $context);
            $result = fpassthru($fp);
            fclose($fp);
        }

        return $result;
    }

    /*
     * Call the HTTP 'POST' method
     * @param string $url URL of the service..
     * @param string $data request data
     * @param array $content_type the http content type
     * @return response string
     */
    public function post($url, $data, $content_type = "application/x-www-form-urlencoded") {
        $result = "";

        if ($this->with_curl) {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_HTTPHEADER, Array (
                "Content-Type: " . $content_type
            ));
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_POST, TRUE);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_USERAGENT, RESTClient :: USER_AGENT);
            $result = curl_exec($curl);
            curl_close($curl);
        } else {
            $opts = array (
                'http' => array (
                    'method' => "POST",
                    'header' => "User-Agent: " . RESTClient :: USER_AGENT . "\r\n" .
                    "Content-Type: " . $content_type . "\r\n" .
                    "Content-length: " . strlen($data
                ) . "\r\n",
                'content' => $data
            ));

            $context = stream_context_create($opts);

            $fp = fopen($url, 'r', false, $context);
            $result = fpassthru($fp);
            fclose($fp);
        }

        return $result;
    }

    /*
     * Call the HTTP 'PUT' method
     * @param string $url URL of the service..
     * @param string $data request data
     * @return response string
     */
    public function put($url, $data) {
        $result = "";

        if ($this->with_curl) {

            $fh = fopen('php://memory', 'rw');
            fwrite($fh, $data);
            rewind($fh);

            $curl = curl_init();

            curl_setopt($curl, CURLOPT_USERAGENT, RESTClient :: USER_AGENT);
            curl_setopt($curl, CURLOPT_INFILE, $fh);
            curl_setopt($curl, CURLOPT_INFILESIZE, strlen($data));
            curl_setopt($curl, CURLOPT_TIMEOUT, 10);
            curl_setopt($curl, CURLOPT_PUT, 1);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            $result = curl_exec($curl);
            curl_close($curl);

            fclose($fh);
        } else {
            $opts = array (
                'http' => array (
                    'method' => "PUT",
                    'header' => "User-Agent: " . RESTClient :: USER_AGENT . "\r\n" .
                    "Content-Type: " . $content_type . "\r\n" .
                    "Content-length: " . strlen($data
                ) . "\r\n",
                'content' => $data
            ));

            $context = stream_context_create($opts);

            $fp = fopen($url, 'r', false, $context);
            $result = fpassthru($fp);
            fclose($fp);
        }

        return $result;
    }

    /*
     * Call the HTTP 'DELETE' method
     * @param string $url URL of the service..
     * @param array $params request parameters, hash of (key,value) pairs
     */
    public function delete($url, $params) {
        $params_str = "?";
        if (is_array($params)) {
            foreach ($params as $key => $value) {
                $params_str .= urlencode($key) . "=" . urlencode($value) . "&";
            }
        } else {
            $params_str .= $params;
        }

        $url .= $params_str;

        $result = "";

        if ($this->with_curl) {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "delete");
            curl_setopt($curl, CURLOPT_USERAGENT, RESTClient :: USER_AGENT);
            $result = curl_exec($curl);
            curl_close($curl);
        } else {
            $opts = array (
                'http' => array (
                    'method' => "DELETE",
                    'header' => "User-Agent: " . RESTClient :: USER_AGENT . "\r\n"
                )
            );

            $context = stream_context_create($opts);

            $fp = fopen($url, 'r', false, $context);
            $result = fpassthru($fp);
            fclose($fp);

        }

    }

}
?>