bigcommerce - how to get values from big commerce using php api -
<?php /** *class instantiate different api connections * * @author adam bilsing <adambilsing@gmail.com> */ class connection { /** *public , private variables * * @var string stores data class */ static public $path; static private $user; static private $token; static private $headers; /** * sets $_path, $_user, $_token, $_headers upon class instantiation * * @param $user, $path, $token required class * @return void */ public function __construct($user, $path, $token) { $path = explode('/api/v2/', $path); $this->_path = $path[0]; $this->_user = $user; $this->_token = $token; $encodedtoken = base64_encode($this->_user.":".$this->_token); $authheaderstring = 'authorization: basic ' . $encodedtoken; $this->_headers = array($authheaderstring, 'accept: application/json','content-type: application/json'); } public static function http_parse_headers( $header ) { $retval = array(); $fields = explode("\r\n", preg_replace('/\x0d\x0a[\x09\x20]+/', ' ', $header)); foreach( $fields $field ) { if( preg_match('/([^:]+): (.+)/m', $field, $match) ) { $match[1] = preg_replace('/(?<=^|[\x09\x20\x2d])./e', 'strtoupper("\0")', strtolower(trim($match[1]))); if( isset($retval[$match[1]]) ) { $retval[$match[1]] = array($retval[$match[1]], $match[2]); } else { $retval[$match[1]] = trim($match[2]); } } } if ($retval['x-bc-apilimit-remaining'] <= 100) { sleep(300); } } public function error($body, $url, $json, $type) { global $error; if (isset($json)) { $results = json_decode($body, true); $results = $results[0]; $results['type'] = $type; $results['url'] = $url; $results['payload'] = $json; $error = $results; } else { $results = json_decode($body, true); $results = $results[0]; $results['type'] = $type; $results['url'] = $url; $error = $results; } } /** * performs request instantiated class * * accepts resource perform request on * * @param $resource string $resource string perform on * @return results or var_dump error */ public function get($resource) { $url = $this->_path . '/api/v2' . $resource; $curl = curl_init(); curl_setopt($curl, curlopt_url, $url); curl_setopt($curl, curlopt_httpheader, $this->_headers); curl_setopt($curl, curlopt_returntransfer, 1); curl_setopt($curl, curlopt_verbose, 1); curl_setopt($curl, curlopt_header, 1); curl_setopt($curl, curlopt_httpget, 1); curl_setopt($curl, curlopt_ssl_verifypeer, false); $response = curl_exec($curl); $http_status = curl_getinfo($curl, curlinfo_http_code); $header_size = curl_getinfo($curl, curlinfo_header_size); $headers = substr($response, 0, $header_size); $body = substr($response, $header_size); self::http_parse_headers($headers); curl_close ($curl); if ($http_status == 200) { $results = json_decode($body, true); return $results; } else { $this->error($body, $url, null, 'get'); } } /** * performs put request instantiated class * * accepts resource perform request on, , fields sent * * @param string $resource string perform on * @param array $fields array sent in request * @return results or var_dump error */ public function put($resource, $fields) { $url = $this->_path . '/api/v2' . $resource; $json = json_encode($fields); $curl = curl_init(); curl_setopt($curl, curlopt_url, $url); curl_setopt($curl, curlopt_httpheader, $this->_headers); curl_setopt($curl, curlopt_returntransfer, true); curl_setopt($curl, curlopt_verbose, 1); curl_setopt($curl, curlopt_header, 1); curl_setopt($curl, curlopt_customrequest, "put"); curl_setopt($curl, curlopt_postfields, $json); curl_setopt($curl, curlopt_ssl_verifypeer, false); $response = curl_exec($curl); $http_status = curl_getinfo($curl, curlinfo_http_code); $header_size = curl_getinfo($curl, curlinfo_header_size); $headers = substr($response, 0, $header_size); $body = substr($response, $header_size); self::http_parse_headers($headers); curl_close($curl); if ($http_status == 200) { $results = json_decode($body, true); return $results; } else { $this->error($body, $url, $json, 'put'); } } /** * performs post request instantiated class * * accepts resource perform request on, , fields sent * * @param string $resource string perform on * @param array $fields array sent in request * @return results or var_dump error */ public function post($resource, $fields) { global $error; $url = $this->_path . '/api/v2' . $resource; $json = json_encode($fields); $curl = curl_init(); curl_setopt($curl, curlopt_url, $url); curl_setopt($curl, curlopt_httpheader, $this->_headers); curl_setopt($curl, curlopt_returntransfer, true); curl_setopt($curl, curlopt_verbose, 1); curl_setopt($curl, curlopt_header, 1); curl_setopt($curl, curlopt_customrequest, "post"); curl_setopt($curl, curlopt_postfields, $json); curl_setopt($curl, curlopt_ssl_verifypeer, false); $response = curl_exec ($curl); $http_status = curl_getinfo($curl, curlinfo_http_code); $header_size = curl_getinfo($curl, curlinfo_header_size); $headers = substr($response, 0, $header_size); $body = substr($response, $header_size); self::http_parse_headers($headers); curl_close ($curl); if ($http_status == 201) { $results = json_decode($body, true); return $results; } else { $this->error($body, $url, $json, 'post'); } } /** * performs delete request instantiated class * * accepts resource perform request on * * @param string $resource string perform on * @return proper response or var_dump error */ public function delete($resource) { $url = $this->_path . '/api/v2' . $resource; $curl = curl_init(); curl_setopt($curl, curlopt_url, $url); curl_setopt($curl, curlopt_httpheader, $this->_headers); curl_setopt($curl, curlopt_returntransfer, true); curl_setopt($curl, curlopt_verbose, 1); curl_setopt($curl, curlopt_header, 1); curl_setopt($curl, curlopt_customrequest, "delete"); curl_setopt($curl, curlopt_ssl_verifypeer, false); $response = curl_exec($curl); $http_status = curl_getinfo($curl, curlinfo_http_code); $header_size = curl_getinfo($curl, curlinfo_header_size); $headers = substr($response, 0, $header_size); $body = substr($response, $header_size); self::http_parse_headers($headers); curl_close ($curl); if ($http_status == 204) { return $http_status . ' deleted'; } else { $this->error($body, $url, null, 'delete'); } } } ?> get('resource'); $store->delete('resource'); $store->post('resource', $fields); $store->put('resource', $fields); ?> if run these code m getting error ( ! ) deprecated: preg_replace(): /e modifier deprecated, use preg_replace_callback instead in d:\wamp\www\bigcommerce\connection.php on line 39
notice: undefined variable: fields in d:\wamp\www\bigcommerce\singlepage.php on line 5
any 1 pls me solve problem
Comments
Post a Comment