PHP utility collection with hybrid and fluent APIs.

⌈⌋ ⎇ branch:  hybrid7 libraries


Check-in [49b2714f5f]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added static $defaults[] array instead of built-in constructor calls. Introduces ->assert() to test CURL/HTTP properties after ->exec(), and possibly drop the result on mismatches.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 49b2714f5fe1143b164d319f6cff7ef998bae2dd
User & Date: mario 2014-08-14 07:39:53
Context
2014-08-14
07:41
ASCII documentation box for db() placeholder syntax and uses. check-in: f9ecfa3ce9 user: mario tags: trunk
07:39
Added static $defaults[] array instead of built-in constructor calls. Introduces ->assert() to test CURL/HTTP properties after ->exec(), and possibly drop the result on mismatches. check-in: 49b2714f5f user: mario tags: trunk
07:38
Restructured directory layout. check-in: 4b0715e805 user: mario tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to curl.php.

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
/**
 * api: php7
 * title: fluent curl
 * description: simple wrapper around curl functions
 * version: 0.2
 * license: Public Domain
 *
 *
 * This simple wrapper class and its factory function allow
 * compact invocations of curl. You can pass either just an
 * URL, an option array, a previously wrapped curl() or a
 * raw curl resource handle.





|







1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
/**
 * api: php7
 * title: fluent curl
 * description: simple wrapper around curl functions
 * version: 0.3
 * license: Public Domain
 *
 *
 * This simple wrapper class and its factory function allow
 * compact invocations of curl. You can pass either just an
 * URL, an option array, a previously wrapped curl() or a
 * raw curl resource handle.
53
54
55
56
57
58
59



















60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90


    /**
     * resource / curl handle
     *
     */
    public $handle = NULL;





















    /**
     * Initialize, where $params is usually just an $url, or an [OPT=>val] list
     *
     */
    function __construct($params) {
    
        // create
        $this->handle = is_object($params) ? $params : curl_init();
        
        // default options
        $this->returntransfer(1)
             ->httpget(1)
             ->http200aliases(array(200,201,202,203))
             ->header(0)
             ->followlocation(!ini_get("safe_mode"));
             
        // option is just URL string
        if (is_string($params)) {
            $this->url($params);
        }
        // multiple [url=>$url, post=>$bool, ..] options
        elseif (is_array($params)) foreach ($params as $cmd=>$opt) {
            $this->__call($cmd, array($opt));
        }
    }

    
    /**
     * Most invocations are mapped onto CURL_CONST settings,







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>











|
<
|
<
|
|
|
<
|
|
|

|







53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

91

92
93
94

95
96
97
98
99
100
101
102
103
104
105
106


    /**
     * resource / curl handle
     *
     */
    public $handle = NULL;
    
    
    /**
     * Setup parameters.
     *
     */
    static $defaults = array(
        "returntransfer" => 1,
        "httpget" => 1,
        "http200aliases" => array(200, 201, 202, 203),
        "header" => 0,
    );


    /**
     * Some checks before returning the resulting content.
     *
     */
    public $assert = array();


    /**
     * Initialize, where $params is usually just an $url, or an [OPT=>val] list
     *
     */
    function __construct($params) {
    
        // create
        $this->handle = is_object($params) ? $params : curl_init();
        
        // merge options

        $params = array_merge(

            curl::$defaults,
            array("followlocation" => !ini_get("safe_mode")),
            is_array($params) ? array_change_key_case($params) : array(),

            is_string($params) ? array("url" => $params) : array()
        );

        // multiple [url=>$url, post=>$bool, ..] options
        if (is_array($params)) foreach ($params as $cmd=>$opt) {
            $this->__call($cmd, array($opt));
        }
    }

    
    /**
     * Most invocations are mapped onto CURL_CONST settings,
108
109
110
111
112
113
114


























115
116
117
118
119
120
121
        # neither exists
        else {
            trigger_error("curl: unknown '$opt' option", E_USER_ERROR);
        }
        
        return $this;
    }



























    
    /**
     * retrieve constants 
     *
     */
    function __get($opt) {







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
        # neither exists
        else {
            trigger_error("curl: unknown '$opt' option", E_USER_ERROR);
        }
        
        return $this;
    }
    
    
    /**
     * Append result-checks such as ["HTTP_CODE" => [200, 201]].
     *
     */
    function assert($list) {
        $this->assert += $list;
        return $this;
    }


    /**
     * Wrap exec() to test result handle for HTTP or CURL properties.
     *
     */
    function exec() {
        $args = func_get_args();
        $content = $this->__call("exec", $args);
        foreach ($this->assert as $option => $values) {
            if (!in_array($this->$option, $values)) {
                return NULL;
            }
        }
        return $content;
    }

    
    /**
     * retrieve constants 
     *
     */
    function __get($opt) {