Last active
August 29, 2015 14:07
-
-
Save cscartjp/d30d6db402512814237b to your computer and use it in GitHub Desktop.
A CodeIgniter library for CS-Cart API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A CodeIgniter library for CS-Cart API | |
* | |
* Example Usage: | |
* | |
* | |
* Methods return a mixture of boolean and stdObjects | |
* | |
* @author Toshihiro Yoshiura <yoshiura@frogman.co.jp> | |
* @license Creative Commons Attribution-Share Alike 3.0 Unported | |
* http://creativecommons.org/licenses/by-sa/3.0/ | |
**/ | |
class Cscart { | |
protected $apiConfig; | |
protected $apiUrl; | |
protected $apiEmail; | |
protected $apiKey; | |
public function __construct($config) { | |
$this->apiConfig = $config; | |
$config_array = $this->apiConfig; | |
$this->apiUrl = $config['url'].'/api'; | |
$this->apiEmail = $config_array['email']; | |
$this->apiKey = $config['api_key']; | |
} | |
/** | |
------------------------------------------------------------------------ | |
PRODUCTS | |
------------------------------------------------------------------------ | |
**/ | |
//商品データを取得する | |
public function get_product($product_id = '0'){ | |
$url = $this->apiUrl.'/products/'.$product_id; | |
$json = $this->get($url); | |
return $json; | |
} | |
//商品データを追加する | |
public function put_product($id, $data){ | |
$json_data = json_encode($data); | |
$url = $this->apiUrl.'/products/'.$id.'/'; | |
$json = $this->put($url, $json_data); | |
return $json; | |
} | |
//商品データを新規追加する | |
public function post_product($data){ | |
$json_data = json_encode($data); | |
$url = $this->apiUrl.'/products/'; | |
$json = $this->post($url, $json_data); | |
return $json; | |
} | |
//商品データを削除する | |
public function delete_product($id){ | |
//$json_data = json_encode($data); | |
$url = $this->apiUrl.'/products/'.$id.'/'; | |
$json = $this->delete($url); | |
return $json; | |
} | |
///CURLリクエスト GET | |
protected function get($host){ | |
// ホストに接続 | |
$ch = curl_init($host); | |
//GET | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');// ★ | |
// TRUE を設定すると出力結果を何も加工せずに返します。 | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
// | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); | |
// 接続に使用するユーザー名とパスワード。 "[username]:[password]" 形式で指定します。 | |
curl_setopt($ch, CURLOPT_USERPWD, $this->apiEmail . ':' . $this->apiKey); | |
// HTTP/1.1 を使用する | |
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | |
$return = curl_exec($ch); | |
curl_close($ch); | |
// 値をJSONデコードして配列化する | |
$json = json_decode($return, true); | |
return $json; | |
} | |
///CURLでUPDATE=PUT | |
protected function put($host, $data){ | |
// ホストに接続 | |
$ch = curl_init($host); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');// ★ | |
// | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); | |
//データ送信 | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);// ★ | |
// TRUE を設定すると出力結果を何も加工せずに返します。 | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
// 接続に使用するユーザー名とパスワード。 "[username]:[password]" 形式で指定します。 | |
curl_setopt($ch, CURLOPT_USERPWD, $this->apiEmail . ':' . $this->apiKey); | |
// HTTP/1.1 を使用する | |
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | |
$return = curl_exec($ch); | |
curl_close($ch); | |
// 値をJSONデコードして配列化する | |
$json = json_decode($return, true); | |
return $json; | |
} | |
///CURLでINSERT=POST | |
protected function post($host, $data){ | |
// ホストに接続 | |
$ch = curl_init($host); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');// ★ | |
// | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); | |
//データ送信 | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);// ★ | |
// TRUE を設定すると出力結果を何も加工せずに返します。 | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
// 接続に使用するユーザー名とパスワード。 "[username]:[password]" 形式で指定します。 | |
curl_setopt($ch, CURLOPT_USERPWD, $this->apiEmail . ':' . $this->apiKey); | |
// HTTP/1.1 を使用する | |
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | |
$return = curl_exec($ch); | |
curl_close($ch); | |
// 値をJSONデコードして配列化する | |
$json = json_decode($return, true); | |
return $json; | |
} | |
///削除 | |
protected function delete($host, $data){ | |
// ホストに接続 | |
$ch = curl_init($host); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');// ★ | |
// | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); | |
//データ送信 | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);// ★ | |
// TRUE を設定すると出力結果を何も加工せずに返します。 | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
// 接続に使用するユーザー名とパスワード。 "[username]:[password]" 形式で指定します。 | |
curl_setopt($ch, CURLOPT_USERPWD, $this->apiEmail . ':' . $this->apiKey); | |
// HTTP/1.1 を使用する | |
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | |
$return = curl_exec($ch); | |
curl_close($ch); | |
// 返り値なし | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment