Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sparkweb committed Jan 26, 2014
1 parent 72cacd9 commit b7d6191
Show file tree
Hide file tree
Showing 9 changed files with 1,062 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
fc-config.php
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
foxycart-api-starter
====================

FoxyCart Hypermedia API Starter
FoxyCart Hypermedia API Starter
=======

This tool is designed to jumpstart your developement with the [FoxyCart Hypermedia API](https://wiki.foxycart.com/v/0.0.0/hypermedia_api).

## Production Note

The API is currently in a limited private beta so you won't be able to get production access for your API Client without working out the details directly with the FoxyCart API support team. If you've already worked out those details, you can create your client and then request that they turn on production access for your client ID. Otherwise, you can use this tool with the API sandbox which will allow you to practice using the API and get familiar with it.

## Setup

1. Make a copy of `fc-config-sample.php` and call it `fc-config.php`. Load your database configuration settings into this file. The db is needed to hold and manage your OAuth tokens.
2. Go to `fc-setup.php` and create your client.
3. If using production, send the ID of the client to FoxyCart and ask for production access.
4. They will send back a Client ID and Client Secret. Load that information into `fc-setup.php` and save.
5. Now load up index.php and start exploring the API.

## Switching to Sandbox

By default, this is set to connect to production. If you want to create a client on the sandbox instead, swith the `$uri` field in `fc-setup.php` and `fc-includes/foxycart-check-tokens.php`. You also need to uncomment a line in the __construct() function of `fc-includes/foxycart-check-tokens.php`.

## Connection Trouble?

If you are having trouble connecting to the FoxyCart servers, your SSL certificate store may not be up to date. The easy way around this is to set the Verify Peer SSL feature to False in these three locations:

- `fc-setup.php`
- `fc-includes/foxycart-check-tokens.php`
- `fc-includes/foxycart-api.php`

## About

This tool was built by David Hollander of [SparkWeb Interactive](http://sparkweb.net) and [FoxyTools](http://foxytools.com). Write to web-AT-sparkweb-DOT-net with questions.
42 changes: 42 additions & 0 deletions fc-client-edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/* Software by David Hollander, foxytools.com */

/*
Need to edit your client? You can do so with this code
*/

if (!include 'fc-config.php') die("Please setup fc-config.php before continuing."); //db settings to store your access and refresh tokens
require "fc-includes/foxycart-api.php"; //class to help communicate with the FoxyCart API
require "fc-includes/foxycart-check-tokens.php"; //function to renew your access tokens if they have expired via OAuth2

//Check access token expiration and refresh if necessary
$access_token = fc_check_token("client"); //<-------- Notice we get the client token, not the store token

//Initialize FoxyCart API
$fc = new FoxyCartApiClient($access_token);

//Get Homepage
$results = $fc->get();
echo "<pre>" . print_r($results, 1) . "</pre>";

//Now we can get client details
$uri = $fc->getLink("client");
if (!$uri) {
die("Could Not Find Client URL");
}
$results = $fc->get($uri);
//echo "<pre>" . print_r($results, 1) . "</pre>";

//Let's Make a Change
$new_data = array(
"company_name" => "Acme",
);

//Get URL
$uri = $fc->getLink("client");

//Patch (updating, POST for creating new)
//$results = $fc->patch($uri, $new_data);

//View Results
//echo "<pre>" . print_r($results, 1) . "</pre>";
10 changes: 10 additions & 0 deletions fc-config-sample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

//Database Helper Class
require "fc-includes/Database.singleton.php";

//Enter your MySQL connection information below
$dbhost = "localhost";
$dblogin = "";
$dbpassword = "";
$dbname = "";
283 changes: 283 additions & 0 deletions fc-includes/Database.singleton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
<?php
# Name: Database.singleton.php
# File Description: MySQL Singleton Class to allow easy and clean access to common mysql commands
# Author: ricocheting
# Web: http://www.ricocheting.com/
# Update: 2010-07-19
# Version: 3.1.4
# Copyright 2003 ricocheting.com


/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


//require("config.inc.php");
//$db = Database::obtain(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);

//$db = Database::obtain();


###################################################################################################
###################################################################################################
###################################################################################################
class Database{

// debug flag for showing error messages
public $debug = true;

// Store the single instance of Database
private static $instance;

private $server = ""; //database server
private $user = ""; //database login name
private $pass = ""; //database login password
private $database = ""; //database name

private $error = "";

#######################
//number of rows affected by SQL query
public $affected_rows = 0;

private $link_id = 0;
private $query_id = 0;


#-#############################################
# desc: constructor
private function __construct($server=null, $user=null, $pass=null, $database=null){
// error catching if not passed in
if($server==null || $user==null || $database==null){
$this->oops("Database information must be passed in when the object is first created.");
}

$this->server=$server;
$this->user=$user;
$this->pass=$pass;
$this->database=$database;
}#-#constructor()


#-#############################################
# desc: singleton declaration
public static function obtain($server=null, $user=null, $pass=null, $database=null){
if (!self::$instance){
self::$instance = new Database($server, $user, $pass, $database);
}

return self::$instance;
}#-#obtain()


#-#############################################
# desc: connect and select database using vars above
# Param: $new_link can force connect() to open a new link, even if mysql_connect() was called before with the same parameters
public function connect($new_link=false){
$this->link_id=@mysql_connect($this->server,$this->user,$this->pass,$new_link);

if (!$this->link_id){//open failed
$this->oops("Could not connect to server: <b>$this->server</b>.");
}

if(!@mysql_select_db($this->database, $this->link_id)){//no database
$this->oops("Could not open database: <b>$this->database</b>.");
}

// unset the data so it can't be dumped
$this->server='';
$this->user='';
$this->pass='';
$this->database='';
}#-#connect()



#-#############################################
# desc: close the connection
public function close(){
if(!@mysql_close($this->link_id)){
$this->oops("Connection close failed.");
}
}#-#close()


#-#############################################
# Desc: escapes characters to be mysql ready
# Param: string
# returns: string
public function escape($string){
if(get_magic_quotes_runtime()) $string = stripslashes($string);
return @mysql_real_escape_string($string,$this->link_id);
}#-#escape()


#-#############################################
# Desc: executes SQL query to an open connection
# Param: (MySQL query) to execute
# returns: (query_id) for fetching results etc
public function query($sql){
// do query
$this->query_id = @mysql_query($sql, $this->link_id);

if (!$this->query_id){
$this->oops("<b>MySQL Query fail:</b> $sql");
return 0;
}

$this->affected_rows = @mysql_affected_rows($this->link_id);

return $this->query_id;
}#-#query()


#-#############################################
# desc: does a query, fetches the first row only, frees resultset
# param: (MySQL query) the query to run on server
# returns: array of fetched results
public function query_first($query_string){
$query_id = $this->query($query_string);
$out = $this->fetch($query_id);
$this->free_result($query_id);
return $out;
}#-#query_first()


#-#############################################
# desc: fetches and returns results one line at a time
# param: query_id for mysql run. if none specified, last used
# return: (array) fetched record(s)
public function fetch($query_id=-1){
// retrieve row
if ($query_id!=-1){
$this->query_id=$query_id;
}

if (isset($this->query_id)){
$record = @mysql_fetch_assoc($this->query_id);
}else{
$this->oops("Invalid query_id: <b>$this->query_id</b>. Records could not be fetched.");
}

return $record;
}#-#fetch()


#-#############################################
# desc: returns all the results (not one row)
# param: (MySQL query) the query to run on server
# returns: assoc array of ALL fetched results
public function fetch_array($sql){
$query_id = $this->query($sql);
$out = array();

while ($row = $this->fetch($query_id)){
$out[] = $row;
}

$this->free_result($query_id);
return $out;
}#-#fetch_array()


#-#############################################
# desc: does an update query with an array
# param: table, assoc array with data (not escaped), where condition (optional. if none given, all records updated)
# returns: (query_id) for fetching results etc
public function update($table, $data, $where='1'){
$q="UPDATE `$table` SET ";

foreach($data as $key=>$val){
if(strtolower($val)=='null') $q.= "`$key` = NULL, ";
elseif(strtolower($val)=='now()') $q.= "`$key` = NOW(), ";
elseif(preg_match("/^increment\((\-?\d+)\)$/i",$val,$m)) $q.= "`$key` = `$key` + $m[1], ";
else $q.= "`$key`='".$this->escape($val)."', ";
}

$q = rtrim($q, ', ') . ' WHERE '.$where.';';

return $this->query($q);
}#-#update()


#-#############################################
# desc: does an insert query with an array
# param: table, assoc array with data (not escaped)
# returns: id of inserted record, false if error
public function insert($table, $data){
$q="INSERT INTO `$table` ";
$v=''; $n='';

foreach($data as $key=>$val){
$n.="`$key`, ";
if(strtolower($val)=='null') $v.="NULL, ";
elseif(strtolower($val)=='now()') $v.="NOW(), ";
else $v.= "'".$this->escape($val)."', ";
}

$q .= "(". rtrim($n, ', ') .") VALUES (". rtrim($v, ', ') .");";

if($this->query($q)){
return mysql_insert_id($this->link_id);
}
else return false;

}#-#insert()


#-#############################################
# desc: frees the resultset
# param: query_id for mysql run. if none specified, last used
private function free_result($query_id=-1){
if ($query_id!=-1){
$this->query_id=$query_id;
}
if($this->query_id!=0 && !@mysql_free_result($this->query_id)){
$this->oops("Result ID: <b>$this->query_id</b> could not be freed.");
}
}#-#free_result()


#-#############################################
# desc: throw an error message
# param: [optional] any custom error to display
private function oops($msg=''){
if(!empty($this->link_id)){
$this->error = mysql_error($this->link_id);
}
else{
$this->error = mysql_error();
$msg="<b>WARNING:</b> No link_id found. Likely not be connected to database.<br />$msg";
}

// if no debug, done here
if(!$this->debug) return;
?>
<table align="center" border="1" cellspacing="0" style="background:white;color:black;width:80%;">
<tr><th colspan=2>Database Error</th></tr>
<tr><td align="right" valign="top">Message:</td><td><?php echo $msg; ?></td></tr>
<?php if(!empty($this->error)) echo '<tr><td align="right" valign="top" nowrap>MySQL Error:</td><td>'.$this->error.'</td></tr>'; ?>
<tr><td align="right">Date:</td><td><?php echo date("l, F j, Y \a\\t g:i:s A"); ?></td></tr>
<?php if(!empty($_SERVER['REQUEST_URI'])) echo '<tr><td align="right">Script:</td><td><a href="'.$_SERVER['REQUEST_URI'].'">'.$_SERVER['REQUEST_URI'].'</a></td></tr>'; ?>
<?php if(!empty($_SERVER['HTTP_REFERER'])) echo '<tr><td align="right">Referer:</td><td><a href="'.$_SERVER['HTTP_REFERER'].'">'.$_SERVER['HTTP_REFERER'].'</a></td></tr>'; ?>
</table>
<?php
}#-#oops()


}//CLASS Database
###################################################################################################

?>
Loading

0 comments on commit b7d6191

Please sign in to comment.