3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Super-simple, minimum abstraction MailChimp API v3 wrapper * * Uses curl if available, falls back to file_get_contents and HTTP stream. * This probably has more comments than code. * * @author Drew McLellan <drew.mclellan@gmail.com> * @version 2.0 */ class MailChimp { private $api_key; private $api_endpoint = 'https://<dc>.api.mailchimp.com/3.0'; private $verify_ssl = true; /** * Create a new instance * @param string $api_key Your MailChimp API key */ public function __construct($api_key) { $this->api_key = $api_key; list(, $datacentre) = explode('-', $this->api_key); $this->api_endpoint = str_replace('<dc>', $datacentre, $this->api_endpoint); } public function delete($method, $args=array(), $timeout=10) { return $this->makeRequest('delete', $method, $args, $timeout); } public function get($method, $args=array(), $timeout=10) { return $this->makeRequest('get', $method, $args, $timeout); } public function patch($method, $args=array(), $timeout=10) { return $this->makeRequest('patch', $method, $args, $timeout); } public function post($method, $args=array(), $timeout=10) { return $this->makeRequest('post', $method, $args, $timeout); } public function put($method, $args=array(), $timeout=10) { return $this->makeRequest('put', $method, $args, $timeout); } /** * Performs the underlying HTTP request. Not very exciting * @param string $$http_verb The HTTP verb to use: get, post, put, patch, delete * @param string $method The API method to be called * @param array $args Assoc array of parameters to be passed * @return array Assoc array of decoded result */ private function makeRequest($http_verb, $method, $args=array(), $timeout=10) { $args['apikey'] = $this->api_key; $url = $this->api_endpoint.'/'.$method; $json_data = json_encode((object)$args); if (function_exists('curl_init') && function_exists('curl_setopt')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, 'drewm:'.$this->api_key); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/vnd.api+json', 'Content-Type: application/vnd.api+json')); curl_setopt($ch, CURLOPT_USERAGENT, 'DrewM/MailChimp-API/3.0 (github.com/drewm/mailchimp-api)'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); switch($http_verb) { case 'post': curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); break; case 'get': $query = http_build_query($args); curl_setopt($ch, CURLOPT_URL, $url.'?'.$query); break; case 'delete': curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); break; case 'patch': curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); break; } $result = curl_exec($ch); curl_close($ch); } else { throw new Exception("cURL support is required, but can't be found."); } return $result ? json_decode($result, true) : false; } } // MailChimp Configuration $api_key = "f36426878616bc7a2e1fc918ab9f9de5-us12"; // ENTER YOUR API KEY HERE $list_id = "d7973804f4"; // ENTER YOUR LIST ID HERE $MailChimp = new MailChimp($api_key); $result = $MailChimp->post('lists/' .$list_id. '/members', array( 'email_address' => 'something@gmail.com', 'status' => 'subscribed' )); if($result['status'] == 'subscribed') { echo json_encode(array('error' => false, 'message' => 'Thanks for subscribing with us')); exit; } else { echo json_encode(array('error' => true, 'message' => $result['title'])); exit; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 22
Branch analysis from position: 16
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 22
1 jumps found. (Code = 79) Position 1 = -2
filename:       /in/A2uEv
function name:  (null)
number of ops:  31
compiled vars:  !0 = $api_key, !1 = $list_id, !2 = $MailChimp, !3 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  117     0  E >   ASSIGN                                                   !0, 'f36426878616bc7a2e1fc918ab9f9de5-us12'
  118     1        ASSIGN                                                   !1, 'd7973804f4'
  120     2        NEW                                              $6      'MailChimp'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !2, $6
  121     6        INIT_METHOD_CALL                                         !2, 'post'
          7        CONCAT                                           ~9      'lists%2F', !1
          8        CONCAT                                           ~10     ~9, '%2Fmembers'
          9        SEND_VAL_EX                                              ~10
  122    10        SEND_VAL_EX                                              <array>
         11        DO_FCALL                                      0  $11     
  121    12        ASSIGN                                                   !3, $11
  125    13        FETCH_DIM_R                                      ~13     !3, 'status'
         14        IS_EQUAL                                                 ~13, 'subscribed'
         15      > JMPZ                                                     ~14, ->22
  126    16    >   INIT_FCALL                                               'json_encode'
         17        SEND_VAL                                                 <array>
         18        DO_ICALL                                         $15     
         19        ECHO                                                     $15
  127    20      > EXIT                                                     
         21*       JMP                                                      ->30
  129    22    >   INIT_FCALL                                               'json_encode'
         23        INIT_ARRAY                                       ~16     <true>, 'error'
         24        FETCH_DIM_R                                      ~17     !3, 'title'
         25        ADD_ARRAY_ELEMENT                                ~16     ~17, 'message'
         26        SEND_VAL                                                 ~16
         27        DO_ICALL                                         $18     
         28        ECHO                                                     $18
  130    29      > EXIT                                                     
  131    30*     > RETURN                                                   1

Class MailChimp:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/A2uEv
function name:  __construct
number of ops:  20
compiled vars:  !0 = $api_key, !1 = $datacentre
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
   25     1        ASSIGN_OBJ                                               'api_key'
          2        OP_DATA                                                  !0
   26     3        INIT_FCALL                                               'explode'
          4        SEND_VAL                                                 '-'
          5        FETCH_OBJ_R                                      ~3      'api_key'
          6        SEND_VAL                                                 ~3
          7        DO_ICALL                                         $4      
          8        FETCH_LIST_R                                     $5      $4, 1
          9        ASSIGN                                                   !1, $5
         10        FREE                                                     $4
   27    11        INIT_FCALL                                               'str_replace'
         12        SEND_VAL                                                 '%3Cdc%3E'
         13        SEND_VAR                                                 !1
         14        FETCH_OBJ_R                                      ~8      'api_endpoint'
         15        SEND_VAL                                                 ~8
         16        DO_ICALL                                         $9      
         17        ASSIGN_OBJ                                               'api_endpoint'
         18        OP_DATA                                                  $9
   28    19      > RETURN                                                   null

End of function __construct

Function delete:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/A2uEv
function name:  delete
number of ops:  11
compiled vars:  !0 = $method, !1 = $args, !2 = $timeout
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
          2        RECV_INIT                                        !2      10
   32     3        INIT_METHOD_CALL                                         'makeRequest'
          4        SEND_VAL_EX                                              'delete'
          5        SEND_VAR_EX                                              !0
          6        SEND_VAR_EX                                              !1
          7        SEND_VAR_EX                                              !2
          8        DO_FCALL                                      0  $3      
          9      > RETURN                                                   $3
   33    10*     > RETURN                                                   null

End of function delete

Function get:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/A2uEv
function name:  get
number of ops:  11
compiled vars:  !0 = $method, !1 = $args, !2 = $timeout
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
          2        RECV_INIT                                        !2      10
   37     3        INIT_METHOD_CALL                                         'makeRequest'
          4        SEND_VAL_EX                                              'get'
          5        SEND_VAR_EX                                              !0
          6        SEND_VAR_EX                                              !1
          7        SEND_VAR_EX                                              !2
          8        DO_FCALL                                      0  $3      
          9      > RETURN                                                   $3
   38    10*     > RETURN                                                   null

End of function get

Function patch:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/A2uEv
function name:  patch
number of ops:  11
compiled vars:  !0 = $method, !1 = $args, !2 = $timeout
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
          2        RECV_INIT                                        !2      10
   42     3        INIT_METHOD_CALL                                         'makeRequest'
          4        SEND_VAL_EX                                              'patch'
          5        SEND_VAR_EX                                              !0
          6        SEND_VAR_EX                                              !1
          7        SEND_VAR_EX                                              !2
          8        DO_FCALL                                      0  $3      
          9      > RETURN                                                   $3
   43    10*     > RETURN                                                   null

End of function patch

Function post:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/A2uEv
function name:  post
number of ops:  11
compiled vars:  !0 = $method, !1 = $args, !2 = $timeout
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
          2        RECV_INIT                                        !2      10
   47     3        INIT_METHOD_CALL                                         'makeRequest'
          4        SEND_VAL_EX                                              'post'
          5        SEND_VAR_EX                                              !0
          6        SEND_VAR_EX                                              !1
          7        SEND_VAR_EX                                              !2
          8        DO_FCALL                                      0  $3      
          9      > RETURN                                                   $3
   48    10*     > RETURN                                                   null

End of function post

Function put:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/A2uEv
function name:  put
number of ops:  11
compiled vars:  !0 = $method, !1 = $args, !2 = $timeout
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
          2        RECV_INIT                                        !2      10
   52     3        INIT_METHOD_CALL                                         'makeRequest'
          4        SEND_VAL_EX                                              'put'
          5        SEND_VAR_EX                                              !0
          6        SEND_VAR_EX                                              !1
          7        SEND_VAR_EX                                              !2
          8        DO_FCALL                                      0  $3      
          9      > RETURN                                                   $3
   53    10*     > RETURN                                                   null

End of function put

Function makerequest:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 20, Position 2 = 24
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 152
Branch analysis from position: 25
6 jumps found. (Code = 188) Position 1 = 98, Position 2 = 111, Position 3 = 124, Position 4 = 131, Position 5 = 144, Position 6 = 89
Branch analysis from position: 98
1 jumps found. (Code = 42) Position 1 = 144
Branch analysis from position: 144
1 jumps found. (Code = 42) Position 1 = 156
Branch analysis from position: 156
2 jumps found. (Code = 43) Position 1 = 157, Position 2 = 163
Branch analysis from position: 157
1 jumps found. (Code = 42) Position 1 = 164
Branch analysis from position: 164
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 163
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 111
1 jumps found. (Code = 42) Position 1 = 144
Branch analysis from position: 144
Branch analysis from position: 124
1 jumps found. (Code = 42) Position 1 = 144
Branch analysis from position: 144
Branch analysis from position: 131
1 jumps found. (Code = 42) Position 1 = 144
Branch analysis from position: 144
Branch analysis from position: 144
Branch analysis from position: 89
2 jumps found. (Code = 44) Position 1 = 91, Position 2 = 98
Branch analysis from position: 91
2 jumps found. (Code = 44) Position 1 = 93, Position 2 = 111
Branch analysis from position: 93
2 jumps found. (Code = 44) Position 1 = 95, Position 2 = 124
Branch analysis from position: 95
2 jumps found. (Code = 44) Position 1 = 97, Position 2 = 131
Branch analysis from position: 97
1 jumps found. (Code = 42) Position 1 = 144
Branch analysis from position: 144
Branch analysis from position: 131
Branch analysis from position: 124
Branch analysis from position: 111
Branch analysis from position: 98
Branch analysis from position: 152
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 24
filename:       /in/A2uEv
function name:  makeRequest
number of ops:  166
compiled vars:  !0 = $http_verb, !1 = $method, !2 = $args, !3 = $timeout, !4 = $url, !5 = $json_data, !6 = $ch, !7 = $query, !8 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   62     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <array>
          3        RECV_INIT                                        !3      10
   64     4        FETCH_OBJ_R                                      ~10     'api_key'
          5        ASSIGN_DIM                                               !2, 'apikey'
          6        OP_DATA                                                  ~10
   66     7        FETCH_OBJ_R                                      ~11     'api_endpoint'
          8        CONCAT                                           ~12     ~11, '%2F'
          9        CONCAT                                           ~13     ~12, !1
         10        ASSIGN                                                   !4, ~13
   68    11        INIT_FCALL                                               'json_encode'
         12        CAST                                          8  ~15     !2
         13        SEND_VAL                                                 ~15
         14        DO_ICALL                                         $16     
         15        ASSIGN                                                   !5, $16
   70    16        INIT_FCALL                                               'function_exists'
         17        SEND_VAL                                                 'curl_init'
         18        DO_ICALL                                         $18     
         19      > JMPZ_EX                                          ~19     $18, ->24
         20    >   INIT_FCALL                                               'function_exists'
         21        SEND_VAL                                                 'curl_setopt'
         22        DO_ICALL                                         $20     
         23        BOOL                                             ~19     $20
         24    > > JMPZ                                                     ~19, ->152
   71    25    >   INIT_FCALL_BY_NAME                                       'curl_init'
         26        DO_FCALL                                      0  $21     
         27        ASSIGN                                                   !6, $21
   72    28        INIT_FCALL_BY_NAME                                       'curl_setopt'
         29        SEND_VAR_EX                                              !6
         30        FETCH_CONSTANT                                   ~23     'CURLOPT_URL'
         31        SEND_VAL_EX                                              ~23
         32        SEND_VAR_EX                                              !4
         33        DO_FCALL                                      0          
   73    34        INIT_FCALL_BY_NAME                                       'curl_setopt'
         35        SEND_VAR_EX                                              !6
         36        FETCH_CONSTANT                                   ~25     'CURLOPT_HTTPAUTH'
         37        SEND_VAL_EX                                              ~25
         38        FETCH_CONSTANT                                   ~26     'CURLAUTH_BASIC'
         39        SEND_VAL_EX                                              ~26
         40        DO_FCALL                                      0          
   74    41        INIT_FCALL_BY_NAME                                       'curl_setopt'
         42        SEND_VAR_EX                                              !6
         43        FETCH_CONSTANT                                   ~28     'CURLOPT_USERPWD'
         44        SEND_VAL_EX                                              ~28
         45        FETCH_OBJ_R                                      ~29     'api_key'
         46        CONCAT                                           ~30     'drewm%3A', ~29
         47        SEND_VAL_EX                                              ~30
         48        DO_FCALL                                      0          
   75    49        INIT_FCALL_BY_NAME                                       'curl_setopt'
         50        SEND_VAR_EX                                              !6
         51        FETCH_CONSTANT                                   ~32     'CURLOPT_HTTPHEADER'
         52        SEND_VAL_EX                                              ~32
         53        SEND_VAL_EX                                              <array>
         54        DO_FCALL                                      0          
   77    55        INIT_FCALL_BY_NAME                                       'curl_setopt'
         56        SEND_VAR_EX                                              !6
         57        FETCH_CONSTANT                                   ~34     'CURLOPT_USERAGENT'
         58        SEND_VAL_EX                                              ~34
         59        SEND_VAL_EX                                              'DrewM%2FMailChimp-API%2F3.0+%28github.com%2Fdrewm%2Fmailchimp-api%29'
         60        DO_FCALL                                      0          
   78    61        INIT_FCALL_BY_NAME                                       'curl_setopt'
         62        SEND_VAR_EX                                              !6
         63        FETCH_CONSTANT                                   ~36     'CURLOPT_RETURNTRANSFER'
         64        SEND_VAL_EX                                              ~36
         65        SEND_VAL_EX                                              <true>
         66        DO_FCALL                                      0          
   79    67        INIT_FCALL_BY_NAME                                       'curl_setopt'
         68        SEND_VAR_EX                                              !6
         69        FETCH_CONSTANT                                   ~38     'CURLOPT_TIMEOUT'
         70        SEND_VAL_EX                                              ~38
         71        SEND_VAR_EX                                              !3
         72        DO_FCALL                                      0          
   80    73        INIT_FCALL_BY_NAME                                       'curl_setopt'
         74        SEND_VAR_EX                                              !6
         75        FETCH_CONSTANT                                   ~40     'CURLOPT_SSL_VERIFYPEER'
         76        SEND_VAL_EX                                              ~40
         77        CHECK_FUNC_ARG                                           
         78        FETCH_OBJ_FUNC_ARG                               $41     'verify_ssl'
         79        SEND_FUNC_ARG                                            $41
         80        DO_FCALL                                      0          
   81    81        INIT_FCALL_BY_NAME                                       'curl_setopt'
         82        SEND_VAR_EX                                              !6
         83        FETCH_CONSTANT                                   ~43     'CURLOPT_HTTP_VERSION'
         84        SEND_VAL_EX                                              ~43
         85        FETCH_CONSTANT                                   ~44     'CURL_HTTP_VERSION_1_0'
         86        SEND_VAL_EX                                              ~44
         87        DO_FCALL                                      0          
   84    88      > SWITCH_STRING                                            !0, [ 'post':->98, 'get':->111, 'delete':->124, 'patch':->131, ], ->144
   85    89    >   IS_EQUAL                                                 !0, 'post'
         90      > JMPNZ                                                    ~46, ->98
   90    91    >   IS_EQUAL                                                 !0, 'get'
         92      > JMPNZ                                                    ~46, ->111
   95    93    >   IS_EQUAL                                                 !0, 'delete'
         94      > JMPNZ                                                    ~46, ->124
   99    95    >   IS_EQUAL                                                 !0, 'patch'
         96      > JMPNZ                                                    ~46, ->131
         97    > > JMP                                                      ->144
   86    98    >   INIT_FCALL_BY_NAME                                       'curl_setopt'
         99        SEND_VAR_EX                                              !6
        100        FETCH_CONSTANT                                   ~47     'CURLOPT_POST'
        101        SEND_VAL_EX                                              ~47
        102        SEND_VAL_EX                                              <true>
        103        DO_FCALL                                      0          
   87   104        INIT_FCALL_BY_NAME                                       'curl_setopt'
        105        SEND_VAR_EX                                              !6
        106        FETCH_CONSTANT                                   ~49     'CURLOPT_POSTFIELDS'
        107        SEND_VAL_EX                                              ~49
        108        SEND_VAR_EX                                              !5
        109        DO_FCALL                                      0          
   88   110      > JMP                                                      ->144
   91   111    >   INIT_FCALL                                               'http_build_query'
        112        SEND_VAR                                                 !2
        113        DO_ICALL                                         $51     
        114        ASSIGN                                                   !7, $51
   92   115        INIT_FCALL_BY_NAME                                       'curl_setopt'
        116        SEND_VAR_EX                                              !6
        117        FETCH_CONSTANT                                   ~53     'CURLOPT_URL'
        118        SEND_VAL_EX                                              ~53
        119        CONCAT                                           ~54     !4, '%3F'
        120        CONCAT                                           ~55     ~54, !7
        121        SEND_VAL_EX                                              ~55
        122        DO_FCALL                                      0          
   93   123      > JMP                                                      ->144
   96   124    >   INIT_FCALL_BY_NAME                                       'curl_setopt'
        125        SEND_VAR_EX                                              !6
        126        FETCH_CONSTANT                                   ~57     'CURLOPT_CUSTOMREQUEST'
        127        SEND_VAL_EX                                              ~57
        128        SEND_VAL_EX                                              'DELETE'
        129        DO_FCALL                                      0          
   97   130      > JMP                                                      ->144
  100   131    >   INIT_FCALL_BY_NAME                                       'curl_setopt'
        132        SEND_VAR_EX                                              !6
        133        FETCH_CONSTANT                                   ~59     'CURLOPT_CUSTOMREQUEST'
        134        SEND_VAL_EX                                              ~59
        135        SEND_VAL_EX                                              'PATCH'
        136        DO_FCALL                                      0          
  101   137        INIT_FCALL_BY_NAME                                       'curl_setopt'
        138        SEND_VAR_EX                                              !6
        139        FETCH_CONSTANT                                   ~61     'CURLOPT_POSTFIELDS'
        140        SEND_VAL_EX                                              ~61
        141        SEND_VAR_EX                                              !5
        142        DO_FCALL                                      0          
  102   143      > JMP                                                      ->144
  106   144    >   INIT_FCALL_BY_NAME                                       'curl_exec'
        145        SEND_VAR_EX                                              !6
        146        DO_FCALL                                      0  $63     
        147        ASSIGN                                                   !8, $63
  107   148        INIT_FCALL_BY_NAME                                       'curl_close'
        149        SEND_VAR_EX                                              !6
        150        DO_FCALL                                      0          
        151      > JMP                                                      ->156
  109   152    >   NEW                                              $66     'Exception'
        153        SEND_VAL_EX                                              'cURL+support+is+required%2C+but+can%27t+be+found.'
        154        DO_FCALL                                      0          
        155      > THROW                                         0          $66
  112   156    > > JMPZ                                                     !8, ->163
        157    >   INIT_FCALL                                               'json_decode'
        158        SEND_VAR                                                 !8
        159        SEND_VAL                                                 <true>
        160        DO_ICALL                                         $68     
        161        QM_ASSIGN                                        ~69     $68
        162      > JMP                                                      ->164
        163    >   QM_ASSIGN                                        ~69     <false>
        164    > > RETURN                                                   ~69
  113   165*     > RETURN                                                   null

End of function makerequest

End of class MailChimp.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
188.57 ms | 1420 KiB | 25 Q